23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

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.

3 Basic Program Components<br />

Function Description Example<br />

any(A)<br />

all(A)<br />

Returns 1 for a vector where any element<br />

of the vector is true (nonzero), and 0 if no<br />

elements are true.<br />

Returns 1 for a vector where all elements of<br />

the vector are true (nonzero), and 0 if all<br />

elements are not true.<br />

any(A) ans = 0<br />

1 1<br />

all(A) ans = 0<br />

1 0<br />

Note The all and any functions ignore any NaN values in the input arrays.<br />

Short-Circuiting in Elementwise Operators. When used in the context of<br />

an if or while expression, and only in this context, the elementwise | and &<br />

operators use short-circuiting in evaluating their expressions. That is, A|B<br />

and A&B ignore the second operand, B, if the first operand, A, issufficientto<br />

determine the result.<br />

So, although the statement 1|[] evaluates to false, thesamestatement<br />

evaluates to true when used in either an if or while expression:<br />

A = 1; B = [];<br />

if(A|B) disp 'The statement is true',<br />

The statement is true<br />

end;<br />

while the reverse logical expression, which does not short-circuit, evaluates<br />

to false<br />

if(B|A) disp 'The statement is true',<br />

end;<br />

Another example of short-circuiting with elementwise operators shows that a<br />

logical expression such as the following, which under most circumstances is<br />

invalid due to a size mismatch between A and B,<br />

A = [1 1]; B = [2 0 1];<br />

A|B % This generates an error.<br />

works within the context of an if or while expression:<br />

if (A|B) disp 'The statement is true',<br />

end;<br />

3-22

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

Saved successfully!

Ooh no, something went wrong!