MATLAB Functions Help Desk

all

Purpose

Test to determine if all elements are nonzero

Syntax

Description

B = all(A) tests whether all the elements along various dimensions of an array are nonzero or logical true (1).

If A is a vector, all(A) returns logical true (1) if all of the elements are nonzero, and returns logical false (0) if one or more elements are zero.

If A is a matrix, all(A) treats the columns of A as vectors, returning a row vector of 1s and 0s.

If A is a multidimensional array, all(A) treats the values along the first non-singleton dimension as vectors, returning a logical condition for each vector.

B = all(A,dim) tests along the dimension of A specified by scalar dim.

Examples

Given,

then B = (A < 0.5) returns logical true (1) only where A is less than one half:

The all function reduces such a vector of logical conditions to a single condition. In this case, all(B) yields 0.

This makes all particularly useful in if statements,

where code is executed depending on a single condition, not a vector of possibly conflicting conditions.

Applying the all function twice to a matrix, as in all(all(A)), always reduces it to a scalar condition.

See Also

The logical operators: &, |, ~, and:

any         Test for any nonzeros

Other functions that collapse an array's dimensions include:

max, mean, median, min, prod, std, sum, trapz



[ Previous | Help Desk | Next ]