Documentation

This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English version of the page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

any

Determine if any array elements are nonzero

Syntax

B = any(A)
B = any(A,'all')
B = any(A,dim)
B = any(A,vecdim)

Description

example

B = any(A) tests along the first array dimension of A whose size does not equal 1, and determines if any element is a nonzero number or logical 1 (true). In practice, any is a natural extension of the logical OR operator.

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

• If A is a nonempty, nonvector matrix, then B = any(A) treats the columns of A as vectors, returning a row vector of logical 1s and 0s.

• If A is an empty 0-by-0 matrix, any(A) returns logical 0 (false).

• If A is a multidimensional array, any(A) acts along the first array dimension whose size does not equal 1 and returns an array of logical values. The size of this dimension becomes 1, while the sizes of all other dimensions remain the same.

B = any(A,'all') tests over all elements of A.

example

B = any(A,dim) tests elements along dimension dim. The dim input is a positive integer scalar.

B = any(A,vecdim) tests elements based on the dimensions specified in the vector vecdim. For example, if A is a matrix, then any(A,[1 2]) tests over all elements in A, since every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

Examples

collapse all

Create a 3-by-3 matrix.

A = [0 0 3;0 0 3;0 0 3]
A = 3×3

0     0     3
0     0     3
0     0     3

Test each column for nonzero elements.

B = any(A)
B = 1x3 logical array

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 = 1x7 logical array

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 1.

This makes any particularly useful in if statements.

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 = logical
1

You can also test the array for elements that are less than zero.

B = any(A(:) < 0)
B = logical
0

The syntax 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.

Create a 3-by-3 matrix.

A = [0 0 3;0 0 3;0 0 3]
A = 3×3

0     0     3
0     0     3
0     0     3

Test the rows of A for nonzero elements by specifying dim = 2.

B = any(A,2)
B = 3x1 logical array

1
1
1

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array. The any function ignores elements of A that are NaN (Not a Number).

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
Complex Number Support: Yes

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, A:

• any(A,1) works on successive elements in the columns of A and returns a row vector of logical values.

• any(A,2) works on successive elements in the rows of A and returns a column vector of logical values.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.

Consider a 2-by-3-by-3 input array, A. Then any(A,[1 2]) returns a 1-by-1-by-3 array whose elements indicate nonzero values for each page of A.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Logical array, returned as a scalar, vector, matrix, or multidimensional array. The dimension of A acted on by any has size 1 in B.