Accelerating the pace of engineering and science

# min

Smallest elements in array

## Description

example

M = min(A) returns the smallest elements of A.

• If A is a vector, then min(A) returns the smallest element of A.

• If A is a matrix, then min(A) treats the columns of A as vectors and returns a row vector of smallest elements.

• If A is a multidimensional array, then min(A) treats the values along the first array dimension whose size does not equal 1 as vectors. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same.

example

M = min(A,[],dim) returns the smallest elements along dimension dim. For example, if A is a matrix, then min(A,[],2) is a column vector containing the minimum value of each row.

example

[M,I] = min(___) finds the indices of the minimum values of A and returns them in output vector I, using any of the input arguments in the previous syntaxes. If there are several identical minimum values, then min returns the index of the first one.

example

C = min(A,B) returns an array the same size as A and B with the smallest elements taken from A or B.

The dimensions of A and B must match, or one can be a scalar.

## Examples

expand all

### Smallest Element in Vector

Return the minimum value from a vector.

```A = [23 42 37 18 52];
M = min(A)```
```M =

18```

### Single Smallest Element in Array

Create a 2-by-3 matrix.

`A = [4 8 2; 9 3 7]`
```A =

4     8     2
9     3     7```

To find the single smallest element in a matrix, you can apply the min function twice, min(min(A)). However, this does not return the index to the smallest element.

Use A(:) to find the minimum element in the column representation of the elements from the array A. Also return the index to the smallest element.

`[M,I] = min(A(:))`
```M =

2

I =

5```

A(:) is a column vector composed of all the columns from the matrix, where each column is appended to the last. A(5) returns the minimum element, 2.

### Minimum Value from Each Matrix Column

Create a 2-by-3 matrix.

`A = [2 8 4; 7 3 9]`
```A =

2     8     4
7     3     9```

Find the minimum of each column.

`[M,I] = min(A)`
```M =

2     3     4

I =

1     2     1
```

A(I(n),n) contains the minimum element in the nth column of A.

### Minimum Value from Each Matrix Row

Create a 2-by-3 matrix.

`A = [1.7 1.2 1.5; 1.3 1.6 1.99]`
```A =

1.7000    1.2000    1.5000
1.3000    1.6000    1.9900```

Find the minimum of each row.

`[M,I] = min(A,[],2)`
```M =

1.2000
1.3000

I =

2
1```

A(n,I(n)) contains the minimum element in the nth row of A.

### Minimum Elements from Two Arrays

Create two arrays of the same size.

```A = [1 8 3; 7 2 9]
B = [2 6 4; 5 2 7]```
```A =

1     8     3
7     2     9

B =

2     6     4
5     2     7
```

Find the minimum elements from A or B.

`C = min(A,B)`
```C =

1     6     3
5     2     7```

C contains the smallest elements from each pair of corresponding elements in A and B.

### Minimum Elements from Array and Scalar

Compare each element of matrix A to a scalar and return the minimum.

Create a 2-by-3 matrix.

`A = [1 7 3; 6 2 9]`
```A =

1     7     3
6     2     9```

Compare each element of A to the scalar value 5.

```B = 5;
C = min(A,B)```
```C =

1     5     3
5     2     5```

C contains the smaller value between each element of A and the scalar 5.

## Input Arguments

expand all

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

Input array, specified as a scalar, vector, matrix, or multidimensional array. A can be a numeric array, logical array, ordinal categorical array, datetime array, or duration array.

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.

Dimension dim indicates the dimension whose length reduces to 1. The size(M,dim) is 1, while the sizes of all other dimensions remain the same, unless size(A,dim) is 0. If size(A,dim) is 0, then min returns an empty array with the same dimension sizes.

Consider a two-dimensional input array, A:

• If dim = 1, then min(A,[],1) returns a row vector containing the smallest element in each column.

• If dim = 2, then min(A,[],2) returns a column vector containing the smallest element in each row.

min returns A if dim is greater than ndims(A).

### B — Second input arrayscalar | vector | matrix | multidimensional array

Second input array, specified as a scalar, vector, matrix, or multidimensional array. B can be a numeric array, logical array, ordinal categorical array, datetime array, or duration array.

The dimensions of A and B must match, or one can be a scalar.

If A and B are ordinal categorical arrays, they must have the same sets of categories, including their order.

A and B can be the same data type or one can be double and combine with single, any integer data type, or duration.

Complex Number Support: Yes

## Output Arguments

expand all

### M — Minimum valuesscalar | vector | matrix | multidimensional array

Minimum values, returned as scalar, vector, matrix, or multidimensional array.

size(M,dim) is 1, while the sizes of all other dimensions match the size of the corresponding dimension in A, unless size(A,dim) is 0. If size(A,dim) is 0, then M is an empty array with the same dimension sizes as A.

### I — Index to Aarray the same size as A

Index to A, returned as an array the same size as A. If the smallest element occurs more than once, then I contains the index to the first occurrence of the value.

### C — Minimum elements from A or Barray

Minimum elements from A or B, returned as an array.

The size of C is as follows:

• If A and B are arrays of the same size, then the size of C matches the size of A and B.

• If either A or B is a scalar, then the size of C matches the size of the nonscalar input array.

The data type of C is as follows:

• If A and B are the same data type, then C matches the data type of A and B.

• If either A or B is single, then C is single.

• If A and B are an integer data type and a scalar double, then C is the same integer data type.

## More About

expand all

### Algorithms

The min function ignores NaNs. When all elements in A are NaNs, then min returns the first one as the minimum. Undefined values in categorical arrays and NaT values in datetime arrays are similar to NaNs in numeric arrays.

When A is complex, the min function returns the complex number with the smallest complex modulus (magnitude). If magnitudes are equal, then min returns the value with the smallest magnitude and the smallest phase angle.

When you use min(A,B) with an empty array and a scalar, then min returns an empty array.

## See Also

Was this topic helpful?