all - Determine if all array elements are nonzero or true - MATLAB (original) (raw)
Determine if all array elements are nonzero or true
Syntax
Description
[B](#btxm2ut-1-B) = all([A](#btxm2ut-1-A))
tests along the first array dimension of A
whose size does not equal 1, and determines if the elements are all nonzero or logical 1
(true
). In practice, all
is a natural extension of the logical AND operator.
- If
A
is a vector, thenall(A)
returns logical1
(true
) if all the elements are nonzero and returns logical0
(false
) if one or more elements are zero. - If
A
is a nonempty matrix, thenall(A)
treats the columns ofA
as vectors and returns a row vector of logical1
s and0
s. - If
A
is an empty 0-by-0 matrix, thenall(A)
returns logical1
(true
). - If
A
is a multidimensional array, thenall(A)
acts along the first array dimension whose size does not equal 1 and returns an array of logical values. The size of this dimension becomes1
, while the sizes of all other dimensions remain the same.
[B](#btxm2ut-1-B) = all([A](#btxm2ut-1-A),`'all'`)
tests over all elements of A
. This syntax is valid for MATLAB® versions R2018b and later.
[B](#btxm2ut-1-B) = all([A](#btxm2ut-1-A),[dim](#btxm2ut-1-dim))
tests elements along dimension dim
. The dim
input is a positive integer scalar.
[B](#btxm2ut-1-B) = all([A](#btxm2ut-1-A),[vecdim](#mw%5F65b75832-c715-492f-987b-96b87c0ca393))
tests elements based on the dimensions specified in the vectorvecdim
. For example, if A
is a matrix, then all(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.
Examples
Create a 3-by-3 matrix, and then test each column for all nonzero elements.
A = 3×3
0 0 3
0 0 3
0 0 3
B = 1×3 logical array
0 0 1
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 = 1×7 logical array
0 0 1 1 1 1 0
The output is a vector of logical values. The all
function reduces such a vector of logical values to a single condition. In this case, B = all(A < 0.5)
yields logical 0
.
This makes all
particularly useful in if
statements.
if all(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.
Create a 3-by-7-by-5 multidimensional array and test to see if all of its elements are less than 3.
A = rand(3,7,5) * 5; B = all(A(:) < 3)
You can also test the array for elements that are greater 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.
Create a 3-by-3 matrix.
A = 3×3
0 0 3
0 0 3
0 0 3
Test the rows of A
for all nonzero elements by specifying dim = 2
.
B = 3×1 logical array
0 0 0
Create a 3-D array and determine if all elements in each page of data (rows and columns) are zero.
A(:,:,1) = [2 1; 3 5]; A(:,:,2) = [0 0; 0 0]; A(:,:,3) = [-2 9; 4 1]; B = all(A,[1 2])
B = 1×1×3 logical array B(:,:,1) =
1
B(:,:,2) =
0
B(:,:,3) =
1
Input Arguments
Input array, specified as a scalar, vector, matrix, or multidimensional array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
Complex Number Support: Yes
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
:
all(A,1)
works on successive elements in the columns ofA
and returns a row vector of logical values.all(A,2)
works on successive elements in the rows ofA
and returns a column vector of logical values.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
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
. Thenall(A,[1 2])
returns a 1-by-1-by-3 array whose elements indicate nonzero values for each page ofA
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Output Arguments
Logical array, returned as a scalar, vector, matrix, or multidimensional array. The dimension of A
acted on by all
has size 1
in B
.
Extended Capabilities
Theall
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- For input argument
A
:- If you do not specify a dimension, the code generator operates along the first dimension of the input array that is variable size or whose size does not equal 1. If this dimension is variable size at code generation time and is 1 at run time, a run-time error can occur. To avoid this error, specify the dimension.
- If all dimensions of the input array are variable size at code generation time, this array cannot be empty at run time.
- If used, the
dim
argument must be a constant at code generation time.
Double data types are not supported.
The all
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).
Version History
Introduced before R2006a