any

Tests whether any element is nonzero (true).

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

B = any(A)

For a vector, tells whether at least one element is nonzero. For a matrix, does so for each column.

B = any(A, dim)

Tests along dimension dim.

B = any(A, 'all')

Tests every element of A and returns one value.

Description

any(x < 0) answers whether a vector has negative entries, and any(isnan(x)) whether it contains NaN. An empty array gives 0, since there is no nonzero element.

For a matrix, any(any(A)) and any(A, 'all') give the same single answer.

Input arguments

A
Numeric, logical or char array.
dim
Dimension to test along.

Output arguments

B
Logical array: 1 where at least one tested element is nonzero.

Example

A = [0 0 1; 0 0 0];
any(A)
any(A, 2)
any(A, 'all')
any([])
Output
ans = 1×3 logical array

   0   0   1
ans = 2×1 logical array

   1
   0
ans = logical
   1
ans = logical
   0

See also

all find isnan nnz