Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
264 <strong>Chapter</strong> 4 <strong>Programm<strong>in</strong>g</strong> <strong>in</strong> <strong>Matlab</strong><br />
>> v=[1 3 6]<br />
v =<br />
1 3 6<br />
>> w=[1 2 4]<br />
w =<br />
1 2 4<br />
The output <strong>of</strong> <strong>the</strong> command v>w will return a logical vector <strong>of</strong> <strong>the</strong> same size as<br />
<strong>the</strong> vectors v and w. Locations where <strong>the</strong> specified relation is true receive a 1<br />
and locations where <strong>the</strong> specified relation is false receive a 0.<br />
>> v>w<br />
ans =<br />
0 1 1<br />
Because <strong>the</strong> first entry <strong>of</strong> v is not larger than <strong>the</strong> first entry <strong>of</strong> w, <strong>the</strong> first entry<br />
<strong>of</strong> <strong>the</strong> answer receives a zero (false). Because <strong>the</strong> second and third entries <strong>of</strong> <strong>the</strong><br />
vector v are larger than <strong>the</strong> second and third entries <strong>of</strong> <strong>the</strong> vector w, <strong>the</strong> second<br />
and third entries <strong>of</strong> <strong>the</strong> answer receive a 1 (true). This is what we mean when we<br />
say “Relational operators always operate element-by-element.”<br />
Like <strong>the</strong> o<strong>the</strong>r array operators we’ve seen that act <strong>in</strong> an element-by-elemnt<br />
manner, <strong>the</strong> arrays be<strong>in</strong>g compared must have <strong>the</strong> same size.<br />
>> u=[10 10]<br />
u =<br />
10 10<br />
>> u>v<br />
??? Error us<strong>in</strong>g ==> gt<br />
Matrix dimensions must agree.<br />
Compar<strong>in</strong>g with a scalar. One exception to <strong>the</strong> “same size rule” is when<br />
an array is compared to a scalar. In this case, <strong>Matlab</strong> tests <strong>the</strong> scalar aga<strong>in</strong>st<br />
every element <strong>of</strong> <strong>the</strong> o<strong>the</strong>r operand.