Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# all

Determine if all array elements are nonzero or true

## Description

example

B = all(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, then all(A) returns logical 1 (true) if all the elements are nonzero and returns logical 0 (false) if one or more elements are zero.

• If A is a nonempty, nonvector matrix, then all(A) treats the columns of A as vectors and returns a row vector of logical 1s and 0s.

• If A is an empty 0-by-0 matrix, then all(A) returns logical 1 (true).

• If A is a multidimensional array, then all(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 becomes 1, while the sizes of all other dimensions remain the same.

example

B = all(A,dim) tests elements along dimension dim. The dim input is a positive integer scalar.

## Examples

expand all

### Test Matrix Columns

Create a 3-by-3 matrix.

```A = [0 0 3;0 0 3;0 0 3]
```
```A =

0     0     3
0     0     3
0     0     3```

Test each column for all nonzero elements.

`B = all(A)`
```B =

0     0     1
```

### Reduce a Logical Vector to a Single Condition

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 =

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.

### Test Arrays of Any Dimension

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)
```
```B =

0```

You can also test the array for elements that are greater than zero.

```B = all(A(:) > 0)
```
```B =

1```

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.

### Test Matrix Rows

Create a 3-by-3 matrix.

```A = [0 0 3;0 0 3;0 0 3]
```
```A =

0     0     3
0     0     3
0     0     3```

Test the rows of A for all nonzero elements by specifying dim = 2.

`B = all(A,2)`
```B =

0
0
0
```

## Input Arguments

expand all

### A — Input Arrayscalar | vector | matrix | multidimensional array

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

### dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, 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 of A and returns a row vector of logical values.

• all(A,2) works on successive elements in the rows of A and returns a column vector of logical values.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

## Output Arguments

expand all

### B — Logical arrayscalar | vector | matrix | multidimensional array

Logical array, returned as a scalar, vector, matrix, or multidimensional array. The dimension of A acted on by all has size 1 in B.