Graphics Programs Reference
In-Depth Information
The OR operator | gives the answer 0 if bothoperands are zero and 1 other-
wise. Thus while the output of relational operators is always 0 or 1 , any
nonzero input to operators suchas & (AND), | (OR), and ~ (NOT) is regarded
by MATLAB to be true, while only 0 is regarded to be false.
If the inputs to a relational operator are vectors or matrices rather than
scalars, then as for arithmetic operations such as + and .* , the operation is
doneterm-by-termandtheoutputisanarrayofzerosandones.Herearesome
examples:
>> [2 3] < [3 2]
ans =
1 0
>> x = -2:2; x >= 0
ans =
0
0
1
1
1
In the second case, x is compared term-by-term to the scalar 0 . Type help
relop or more information.
Youcanusethefactthattheoutputofarelationaloperatorisalogicalarray
to select the elements of an array that meet a certain condition. For example,
the expression x(x >= 0) yields a vector consisting of only the nonnegative
elementsof x (ormoreprecisely,thosewithnonzerorealpart).So,if x = -2:2
as above,
>> x(x >= 0)
ans =
0 1 2
If a logical array is used to choose elements from another array, the two arrays
musthavethesamesize.Theelementscorrespondingtotheonesinthelogical
array are selected while the elements corresponding to the zeros are not. In
the example above, the result is the same as if we had typed x(3:5) , but in
this case 3:5 is an ordinary numerical array specifying the numerical indices
of the elements to choose.
Next, we discuss how if and elseif decide whether an expression is true
or false. For an expression that evaluates to a scalar real number, the criterion
is the same as described above — namely, a nonzero number is treated as true
while 0 is treated as false. However, for complex numbers only the real part
is considered. Thus, in an if or elseif statement, any number withnonzero
Search WWH ::




Custom Search