all

Tests whether all elements are nonzero (true).

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

B = all(A)

For a vector, tells whether all elements are nonzero. For a matrix, does so for each column.

B = all(A, dim)

Tests along dimension dim.

B = all(A, 'all')

Tests every element of A and returns one value.

Description

all reduces a logical array along a dimension. A typical use is all(x > 0), which checks a condition for the whole vector. For an empty array the answer is 1, because no element violates the condition.

NaN counts as nonzero, so all(NaN) is true. Use any for the opposite question: whether at least one element is nonzero.

Input arguments

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

Output arguments

B
Logical array: 1 where every tested element is nonzero.

Example

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

   1   1   0
ans = 2×1 logical array

   0
   1
ans = logical
   0
ans = logical
   1

See also

any find isequal nnz