next up previous contents
Next: Control Up: Flow Previous: Flow


MATLAB provides these logical operators:


and these boolean relations:


The result of applying any of these to a scalar or pair of scalars (as appropriate) is always a 1 (True) or a 0 (False). They operate element-wise on matrices; there are no ``full matrix'' versions. To test whether a relation holds over an entire matrix, or whether it holds anywhere in the matrix, the commands any and all are useful. Each takes a vector; any ORs the elements (true iff any are true) and all ANDs the elements (true iff all are true). They operate column-by-column on a matrix to produce a vector. So, for instance,

.1ex>> all(all(A == B))
.1ex>> any(any(A tex2html_wrap_inline2377 = B))

both test for whether the matrices A and B are entirely equal, the former returning 1 if so (because every element in every column matches), the latter returning 1 if not (because some element in some column did not match). The function find will return the indices of the non-zero (True) elements of a matrix; this is useful for selective indexing.

There are also a number of boolean tests for various characteristics; see help ops for most of them.

Sat Mar 21 21:42:28 EST 1998