Programming Previous page   Next Page

Creating a Logical Array

One way of creating an array of logicals is to just enter a true or false value for each element. The true function returns logical one; the false function returns logical zero:

Logical Operations on an Array

You can also perform some logical operation on an array that yields an array of logicals:

The MATLAB functions that have names beginning with is (e.g., ischar, issparse) also return a logical value or array:

This table shows some of the MATLAB operations that return a logical true or false.

true, false
Setting value to true or false
Numeric to logical conversion
& (and), | (or), ~ (not), xor, any, all
Logical operations
&&, ||
Short-circuit AND and OR
== (eq), ~= (ne), < (lt), > (gt),
<= (le), >= (ge)
Relational operations
All is* functions, cellfun
Test operations
strcmp, strncmp, strcmpi, strncmpi
String comparisons

Sparse Logical Arrays

Logical arrays can also be sparse as long as they have no more than two dimensions:

Previous page  Logical Types How Logical Arrays Are Used Next page

© 1994-2005 The MathWorks, Inc.