MATLAB Function Reference Previous page   Next Page

Determine if any array elements are nonzero



B = any(A) tests whether any of the elements along various dimensions of an array is a nonzero number or is logical 1 (true). any ignores entries that are NaN (Not a Number).

If A is a vector, any(A) returns logical 1 (true) if any of the elements of A is a nonzero number or is logical 1 (true), and returns logical 0 (false) if all the elements are zero.

If A is a matrix, any(A) treats the columns of A as vectors, returning a row vector of logical 1's and 0's.

If A is a multidimensional array, any(A) treats the values along the first nonsingleton dimension as vectors, returning a logical condition for each vector.

B = any(A, dim) tests along the dimension of A specified by scalar dim.



then B = (A < 0.5) returns logical 1 (true) only where A is less than one half:

The any function reduces such a vector of logical conditions to a single condition. In this case, any(B) yields logical 1.

This makes any particularly useful in if statements:

where code is executed depending on a single condition, not a vector of possibly conflicting conditions.

Applying the any function twice to a matrix, as in any(any(A)), always reduces it to a scalar condition.

See Also

all, logical operators (elementwise and short-circuit), relational operators, colon

Other functions that collapse an array's dimensions include max, mean, median, min, prod, std, sum, and trapz.

Previous page  ans area Next page

© 1994-2005 The MathWorks, Inc.