# min

Smallest elements in array

## Syntax

• `M = min(A)` example
• `M = min(A,[],dim)` example
• ```[M,I] = min(___)``` example
• `___ = min(___,nanflag)` example

## 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)` is a row vector containing the minimum value of each column.If `A` is a multidimensional array, then `min(A)` operates along the first array dimension whose size does not equal `1`, treating the elements as vectors. The size of this dimension becomes `1` while the sizes of all other dimensions remain the same. If `A` is an empty array with first dimension `0`, then `min(A)` returns an empty array of the same size as `A`.```

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 the minimum value occurs more than once, then `min` returns the index corresponding to the first occurrence.```

example

````C = min(A,B)` returns an array the same size as `A` and `B` with the smallest elements taken from `A` or `B`.Either the dimensions of `A` and `B` are the same, or one can be a scalar.```

example

````___ = min(___,nanflag)` specifies whether to include or omit `NaN` values in the calculation for any of the previous syntaxes. For the single input case, to specify `nanflag` without specifying `dim`, use `min(A,[],nanflag)`. For example, `min(A,[],'includenan')` includes all `NaN` values in `A` while `min(A,[],'omitnan')` ignores them.```

## Examples

collapse all

### Smallest Vector Element

Create a vector and compute its smallest element.

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

### Smallest Complex Element

Create a complex vector and compute its smallest element, that is, the element with the smallest magnitude.

```A = [-2+2i 4+i -1-3i]; min(A) ```
```ans = -2.0000 + 2.0000i ```

### Smallest Element in Each Matrix Column

Create a matrix and compute the smallest element in each column.

```A = [2 8 4; 7 3 9] ```
```A = 2 8 4 7 3 9 ```
```M = min(A) ```
```M = 2 3 4 ```

### Smallest Element in Each Matrix Row

Create a matrix and compute the smallest element in each row.

```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 ```
```M = min(A,[],2) ```
```M = 1.2000 1.3000 ```

### Smallest Element Indices

Create a matrix `A` and compute the smallest elements in each column as well as the row indices of `A` in which they appear.

```A = [1 9 -2; 8 4 -5] ```
```A = 1 9 -2 8 4 -5 ```
```[M,I] = min(A) ```
```M = 1 4 -5 I = 1 2 2 ```

### Smallest Element Comparison

Create a matrix and return the smallest value between each of its elements compared to a scalar.

```A = [1 7 3; 6 2 9] ```
```A = 1 7 3 6 2 9 ```
```B = 5; C = min(A,B) ```
```C = 1 5 3 5 2 5 ```

### Smallest Element in Matrix

Create a matrix `A` and use its column representation, `A(:)`, to find the value and index of the smallest element.

```A = [8 2 4; 7 3 9] ```
```A = 8 2 4 7 3 9 ```
```A(:) ```
```ans = 8 7 2 3 4 9 ```
```[M,I] = min(A(:)) ```
```M = 2 I = 3 ```

`I` is the index of `A(:)` containing the smallest element.

Now, use the `ind2sub` function to extract the row and column indices of `A` corresponding to the smallest element.

```[I_row, I_col] = ind2sub(size(A),I) ```
```I_row = 1 I_col = 2 ```

If you need only the minimum value of `A` and not its index, call the `min` function twice.

```M = min(min(A)) ```
```M = 2 ```

### Smallest Element Involving `NaN`

Create a vector and compute its minimum, excluding `NaN` values.

```A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19]; M = min(A,[],'omitnan') ```
```M = -2.9500 ```

`min(A)` will also produce this result since `'omitnan'` is the default option.

Use the `'includenan'` flag to return `NaN`.

```M = min(A,[],'includenan') ```
```M = NaN ```

## Input Arguments

collapse all

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

Input array, specified as a scalar, vector, matrix, or multidimensional array.

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

• If `A` is a scalar, then `min(A)` returns `A`.

• If `A` is a 0-by-0 empty array, then `min(A)` is as well.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical` | `categorical` | `datetime` | `duration`
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, then 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` — Additional input arrayscalar | vector | matrix | multidimensional array

Additional input array, specified as a scalar, vector, matrix, or multidimensional array.

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

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

• If `A` and `B` are ordinal categorical arrays, they must have the same sets of categories with the same order.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical` | `categorical` | `datetime` | `duration`
Complex Number Support: Yes

### `nanflag` — `NaN` condition‘`omitnan`' (default) | ‘`includenan`'

`NaN` condition, specified as one of these values:

• `omitnan`' — Ignore all `NaN` values in the input.

• `includenan`' — Include the `NaN` values in the input for the calculation.

The `min` function does not support the `nanflag` option for `datetime`, `duration`, or `categorical` arrays.

Data Types: `char`

## Output Arguments

collapse all

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

Minimum values, returned as a 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 dimensions as `A`.

### `I` — Index to minimum values of `A`scalar | vector | matrix | multidimensional array

Index to minimum values of `A`, returned as a scalar, vector, matrix, or multidimensional array. `I` is the same size as `M`. 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 `B`scalar | vector | matrix | multidimensional array

Minimum elements from `A` or `B`, returned as a scalar, vector, matrix, or multidimensional array.

The size of `C` depends on the sizes of `A` and `B`:

• 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.

• If either `A` or `B` is an empty array with the other a scalar, then `C` is an empty array.

The data type of `C` depends on the data types of `A` and `B`:

• 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 either `A` or `B` is an integer data type with the other a scalar `double`, then `C` assumes the integer data type.