MATLAB Functions | Help Desk |
any
Test for any nonzeros
B = any(A)
B = any(A,dim
)
B = any(A)
tests whether any of the elements along various dimensions of an array are nonzero or logical true (1
).
If A
is a vector, any(A)
returns logical true (1
) if any of the elements of A
are nonzero, and returns logical false (0
) 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 1
s and 0
s.
If A
is a multidimensional array, any(A)
treats the values along the first non-singleton dimension as vectors, returning a logical condition for each vector.
B = any(A,dim
)
tests along the dimension of A
specified by scalar dim
.A = [0.53 0.67 0.01 0.38 0.07 0.42 0.69]
then B = (A < 0.5)
returns logical true (1
) only where A
is less than one half:
0 0 1 1 1 1 0The
any
function reduces such a vector of logical conditions to a single condition. In this case, any(B) yields 1
.
This makes any
particularly useful in if
statements,
if any(A < 0.5)
do something
end
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.
any(any(eye(3))) ans = 1The logical operators
&
, |
, ~
, and:
all
Test to determine if all elements are nonzero
Other functions that collapse an array's dimensions include:
max
, mean
, median
, min
, prod
, std
, sum
, trapz