Skip to content

all

Determine if all elements are non-zero

b = all(A)

  • When A is a scalar, b is logical 1 if A is non-zero, and logical 0 if otherwise.
  • When A is a vector, b is logical 1 if all elements of A are non-zero, and logical 0 if otherwise.
  • When A is a matrix or an array of ndims(A) >= 3, an element of b is assigned logical 1 if elements of the corresponding vector of A along the first non-singleton dimension are all non-zero, and logical 0 if otherwise.
  • all(A) collapses the first non-singleton dimension of A. That means, if size(A) = [s1 s2, ..., sN], then size(b) = [1 s2, s3, ..., sN]. For example, b(1,1,1) = 1 if A(:,1,1) are all non-zero.

Example 1: It checks if a has a column with all non-zero entries.

a=randi(3,3,3)-2
all(a)
a =
 1.000  -1.000  -1.000
-1.000   0.000  -1.000
 0.000  -1.000  -1.000

ans =
0  0  1

b = all(A, dim)

  • dim should be a positive integer.
  • If the dim-th dimension of A is non-singleton, an element of b is assigned logical 1 if elements of the corresponding vector of A along the dim-th dimension are all non-zero, and logical 0 if otherwise.

Example 2: It checks if a has a row with all non-zero entries.

a=randi(3,3,3)-2
all(a,2)
a =
 1.000  -1.000  -1.000
-1.000   0.000  -1.000
 0.000  -1.000  -1.000

ans =
1
0
0