06.07.2013 Views

Scilab Bag Of Tricks - Claymore

Scilab Bag Of Tricks - Claymore

Scilab Bag Of Tricks - Claymore

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

(1.) / ([1 2 3])<br />

Chapter 2. Pitfalls<br />

where the parentheses have been introduced for clarity. This behavior is described in SCI/README,<br />

and in the <strong>Scilab</strong> FAQ .<br />

We suggest to avoid whitespace that influences the calculation by not letting the decimal point stick<br />

out on either side. That way expressions with numerals will always be interpreted correctly. For our<br />

example this means<br />

->1.0./ [1 2 3]<br />

ans =<br />

! 1. 0.5 0.3333333 !<br />

which gives what we had in mind.<br />

2.2. Vector Construction<br />

The square bracket operator [ ] is a convenient means to construct vectors. There even exists an<br />

idiom to build a matrix with brackets, which is shown in Example 2-1.<br />

Example 2-1. Building a matrix column-by-column and row-by-row<br />

mat = []<br />

for i = 1:n<br />

row = []<br />

for j = 1:m<br />

... – compute matrix entry<br />

expr = ...<br />

row = [row expr]<br />

end<br />

mat = [mat; row]<br />

end<br />

Rows are separated by semi-colons or newlines, which actually is straight forward. Columns are<br />

separated by commas, or spaces—and here comes trouble. First, comma and space serve the same<br />

purpose, and are interchangeable. Thus, the following expressions have the same result.<br />

[1 2 3 4]<br />

[1,2,3,4]<br />

[1 2 3,4]<br />

[ 1, 2 3 , 4 ]<br />

Second, a space is sometimes considered a column-separating space, sometimes a intra-expression<br />

space. This can lead to some confusion as the following three matrix definitions demonstrate. Who<br />

gets all of them right without peeking at the answers?<br />

20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!