min

Minimum elements of an array

Description

example

M = min(A) returns the minimum elements of an array.

• If A is a vector, then min(A) returns the minimum 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 with the same size as A.

example

M = min(A,[],dim) returns the minimum element 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 = min(A,[],nanflag) specifies whether to include or omit NaN values in the calculation. For example, min(A,[],'includenan') includes all NaN values in A while min(A,[],'omitnan') ignores them.

M = min(A,[],dim,nanflag) also specifies the dimension to operate along when using the nanflag option.

example

[M,I] = min(___) also returns the index into the operating dimension that corresponds to the minimum value of A for any of the previous syntaxes.

example

M = min(A,[],'all') finds the minimum over all elements of A. This syntax is valid for MATLAB® versions R2018b and later.

example

M = min(A,[],vecdim) computes the minimum over the dimensions specified in the vector vecdim. For example, if A is a matrix, then min(A,[],[1 2]) computes the minimum over all elements in A, since every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

M = min(A,[],'all',nanflag) computes the minimum over all elements of A when using the nanflag option.

M = min(A,[],vecdim,nanflag) specifies multiple dimensions to operate along when using the nanflag option.

[M,I] = min(A,[],'all',___) returns the linear index into A that corresponds to the minimum value in A when specifying 'all'.

example

[M,I] = min(A,[],___,'linear') returns the linear index into A that corresponds to the minimum value in A.

example

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

C = min(A,B,nanflag) also specifies how to treat NaN values.

___ = min(___,'ComparisonMethod',method) optionally specifies how to compare elements for any of the previous syntaxes. For example, for a vector A = [-1 2 -9], the syntax min(A,[],'ComparisonMethod','abs') compares the elements of A according to their absolute values and returns -1.

Examples

collapse all

Create a vector and compute its smallest element.

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

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

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

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

2     8     4
7     3     9

M = min(A)
M = 1×3

2     3     4

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 = 2×3

1.7000    1.2000    1.5000
1.3000    1.6000    1.9900

M = min(A,[],2)
M = 2×1

1.2000
1.3000

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

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 = 2×3

1     9    -2
8     4    -5

[M,I] = min(A)
M = 1×3

1     4    -5

I = 1×3

1     2     2

Create a 3-D array and compute the minimum over each page of data (rows and columns).

A(:,:,1) = [2 4; -2 1];
A(:,:,2) = [9 13; -5 7];
A(:,:,3) = [4 4; 8 -3];
M1 = min(A,[],[1 2])
M1 =
M1(:,:,1) =

-2

M1(:,:,2) =

-5

M1(:,:,3) =

-3

Starting in R2018b, to compute the minimum over all dimensions of an array, you can either specify each dimension in the vector dimension argument, or use the 'all' option.

M2 = min(A,[],[1 2 3])
M2 = -5
Mall = min(A,[],'all')
Mall = -5

Create a matrix A and return the minimum value of each row in the matrix M. Use the 'linear' option to also return the linear indices I such that M = A(I).

A = [1 2 3; 4 5 6]
A = 2×3

1     2     3
4     5     6

[M,I] = min(A,[],2,'linear')
M = 2×1

1
4

I = 2×1

1
2

minvals = A(I)
minvals = 2×1

1
4

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 = 2×3

1     7     3
6     2     9

B = 5;
C = min(A,B)
C = 2×3

1     5     3
5     2     5

Input Arguments

collapse all

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.

If A has type categorical, then it must be ordinal.

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

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(A,dim) returns an empty array with the same size as A.

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

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. Then min(A,[],[1 2]) returns a 1-by-1-by-3 array whose elements are the minimums computed over each page of A. Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Additional input array, specified as a scalar, vector, matrix, or multidimensional array. Inputs A and B must either be the same size or have sizes that are compatible (for example, A is an M-by-N matrix and B is a scalar or 1-by-N row vector). For more information, see Compatible Array Sizes for Basic Operations.

• A and B must be the same data type unless one is a double. In that case, the data type of the other array can be single, duration, or any integer 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

NaN condition, specified as one of these values:

• 'omitnan' — Ignore all NaN values in the input. If all elements are NaN, then min returns the first one.

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

For datetime arrays, you can also use 'omitnat' or 'includenat' to omit and include NaT values, respectively.

For categorical arrays, you can also use 'omitundefined' or 'includeundefined' to omit and include undefined values, respectively.

Data Types: char

Comparison method for numeric input, specified as one of these values:

• 'auto' — For a numeric input array A, compare elements by real(A) when A is real, and by abs(A) when A is complex.

• 'real' — For a numeric input array A, compare elements by real(A) when A is real or complex. If A has elements with equal real parts, then use imag(A) to break ties.

• 'abs' — For a numeric input array A, compare elements by abs(A) when A is real or complex. If A has elements with equal magnitude, then use angle(A) in the interval (-π,π] to break ties.

Output Arguments

collapse all

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 size as A.

Index, returned as a scalar, vector, matrix, or multidimensional array. I is the same size as the first output.

When 'linear' is not specified, I is the index into the operating dimension. When 'linear' is specified, I contains the linear indices of A corresponding to the minimum values.

If the smallest element occurs more than once, then I contains the index to the first occurrence of the value.

Minimum elements from A or B, returned as a scalar, vector, matrix, or multidimensional array. The size of C is determined by implicit expansion of the dimensions of A and B. For more information, see Compatible Array Sizes for Basic Operations.

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.