23.12.2012 Views

MATLAB Function Reference Volume 1: A - E - Bad Request

MATLAB Function Reference Volume 1: A - E - Bad Request

MATLAB Function Reference Volume 1: A - E - Bad Request

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.

Logical Operators & | ~<br />

expression a|b&c is evaluated as a|(b&c). It is a good idea to use parentheses<br />

to explicitly specify the intended precedence of statements containing<br />

combinations of & and |.<br />

Partial Evaluation<br />

Within the context of an if or while expression, <strong>MATLAB</strong> does not necessarily<br />

evaluate all parts of a logical expression. In some cases it is possible, and often<br />

advantageous, to determine whether an expression is true or false through only<br />

partial evaluation.<br />

For example, if A equals zero in statement 1 below, then the expression<br />

evaluates to false, regardless of the value of B. In this case, there is no need to<br />

evaluate B and <strong>MATLAB</strong> does not do so. In statement 2, if A is nonzero, then<br />

the expression is true, regardless of B. Again, <strong>MATLAB</strong> does not evaluate the<br />

latter part of the expression.<br />

1) if (A & B) 2) if (A | B)<br />

You can use this property to your advantage to cause <strong>MATLAB</strong> to evaluate a<br />

part of an expression only if a preceding part evaluates to the desired state.<br />

Examples Here are some examples of using partial evaluation.<br />

while (b ~= 0) & (a/b > 18.5)<br />

if exist('myfun.m') & (myfun(x) >= y)<br />

if iscell(A) & all(cellfun('isreal', A))<br />

See Also all, any, find, logical, xor<br />

The relational operators: =, ==, ~=ì<br />

2-21

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

Saved successfully!

Ooh no, something went wrong!