Determine if any array elements are nonzero
along the first array dimension of
B = any(
A whose size
does not equal 1, and determines if any element is a nonzero number
true). In practice,
a natural extension of the logical OR operator.
A is a vector, then
= any(A) returns logical
if any of the elements of
A is a nonzero number
or is logical
1, and returns logical
if all the elements are zero.
A is a nonempty, nonvector matrix,
B = any(A) treats the columns of
vectors, returning a row vector of logical
A is an empty 0-by-0 matrix,
A is a multidimensional array,
along the first array dimension whose size does not equal 1 and returns
an array of logical values. The size of this dimension becomes
while the sizes of all other dimensions remain the same.
Create a 3-by-3 matrix.
A = [0 0 3;0 0 3;0 0 3]
A = 0 0 3 0 0 3 0 0 3
Test each column for nonzero elements.
B = any(A)
B = 0 0 1
Create a vector of decimal values and test which values are less than 0.5.
A = [0.53 0.67 0.01 0.38 0.07 0.42 0.69]; B = (A < 0.5)
B = 0 0 1 1 1 1 0
The output is a vector of logical values. The
any function reduces such a vector of logical values to a single condition. In this case,
B = any(A < 0.5) yields logical
any particularly useful in
if any(A < 0.5) %do something else %do something else end
The code is executed depending on a single condition, rather than a vector of possibly conflicting conditions.
Create a 3-by-7-by-5 multidimensional array and test to see if any of its elements are greater than 3.
A = rand(3,7,5) * 5; B = any(A(:) > 3)
B = 1
You can also test the array for elements that are less than zero.
B = any(A(:) < 0)
B = 0
A(:) turns the elements of
A into a single column vector, so you can use this type of statement on an array of any size.
A— Input arrayscalar | vector | matrix | multidimensional array
Input array, specified as a scalar, vector, matrix, or multidimensional
any function ignores elements of
NaN (Not a Number).
Complex Number Support: Yes
dim— Dimension to operate alongpositive integer scalar
Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.
Consider a two-dimensional input array,
any(A,1) works on successive elements
in the columns of
A and returns a row vector of
any(A,2) works on successive elements
in the rows of
A and returns a column vector of