any - Determine if any array elements are nonzero - MATLAB (original) (raw)

Determine if any array elements are nonzero

Syntax

Description

[B](#btwohgt-1-B) = any([A](#btwohgt-1-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.

example

[B](#btwohgt-1-B) = any([A](#btwohgt-1-A),`'all'`) tests over all elements of A. This syntax is valid for MATLAB® versions R2018b and later.

[B](#btwohgt-1-B) = any([A](#btwohgt-1-A),[dim](#btwohgt-1-dim)) tests elements along dimension dim. The dim input is a positive integer scalar.

example

[B](#btwohgt-1-B) = any([A](#btwohgt-1-A),[vecdim](#mw%5Fd4f38e84-f63a-4002-ad9b-9ce266aa26ad)) tests elements based on the dimensions specified in the vectorvecdim. For example, if A is a matrix, then any(A,[1 2]) tests over all elements inA, since every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

Examples

collapse all

Test Matrix Columns

Create a 3-by-3 matrix.

A = 3×3

 0     0     3
 0     0     3
 0     0     3

Test each column for nonzero elements.

B = 1x3 logical array

0 0 1

Reduce a Logical Vector to a Single Condition

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.

Test Arrays of Any Dimension

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)

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

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.

Test Matrix Rows

Create a 3-by-3 matrix.

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 = 3x1 logical array

1 1 1

Nonzero Elements in Array Page

Create a 3-D array and determine if there are nonzero elements in each page of data (rows and columns).

A(:,:,1) = [2 0; 0 0]; A(:,:,2) = [0 0; 0 0]; A(:,:,3) = [0 0; 0 1]; B = any(A,[1 2])

B = 1x1x3 logical array B(:,:,1) =

1

B(:,:,2) =

0

B(:,:,3) =

1

Input Arguments

collapse all

A — Input array

scalar | vector | matrix | multidimensional array

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

dim — Dimension to operate along

positive integer scalar

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension whose size does not equal 1.

Consider a two-dimensional input array, A:

any(A,1) column-wise computation and any(A,2) row-wise computation.

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

vecdim — Vector of dimensions

vector of positive integers

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. Thenany(A,[1 2]) returns a 1-by-1-by-3 array whose elements indicate nonzero values for each page ofA.

any(A,[1 2]) collapses the pages of a 2-by-3-by-3 array into a 1-by-1-by-3 array.

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

Output Arguments

collapse all

B — Logical array

scalar | vector | matrix | multidimensional array

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

Extended Capabilities

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

Theany function fully supports tall arrays. For more information, see Tall Arrays.

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

HDL Code Generation

Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.

Double data types are not supported.

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

The any function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a