min - Minimum elements of array - MATLAB (original) (raw)

Minimum elements of array

Syntax

Description

[M](#bua94lg-1-M) = min([A](#bua94lg-1-A)) returns the minimum elements of an array.

example

[M](#bua94lg-1-M) = min([A](#bua94lg-1-A),[],`"all"`) returns the minimum over all elements of A.

example

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

example

[M](#bua94lg-1-M) = min([A](#bua94lg-1-A),[],[vecdim](#mw%5F3e43333b-fd64-440c-8fdf-f4a4ff2fb496)) returns the minimum over the dimensions specified in the vectorvecdim. For example, if A is a matrix, then min(A,[],[1 2]) returns the minimum over all elements inA because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

[M](#bua94lg-1-M) = min([A](#bua94lg-1-A),[],___,[missingflag](#mw%5F323e241b-f738-432a-a407-88a22bf9d13e)) specifies whether to omit or include missing values in A for any of the previous syntaxes. For example,min(A,[],"includemissing") includes all missing values when computing the minimum. By default, min omits missing values.

example

[[M](#bua94lg-1-M),[I](#mw%5F8c71663d-dc12-403c-b39d-d33960f3e3ae)] = min(___) also returns the index into the operating dimension that corresponds to the first occurrence of the minimum value of A.

example

[[M](#bua94lg-1-M),[I](#mw%5F8c71663d-dc12-403c-b39d-d33960f3e3ae)] = min([A](#bua94lg-1-A),[],___,"linear") also returns the linear index into A that corresponds to the minimum value in A.

example

[C](#bua94lg-1-C) = min([A](#bua94lg-1-A),[B](#bua94lg-1-B)) returns an array with the smallest elements taken from A orB.

example

[C](#bua94lg-1-C) = min([A](#bua94lg-1-A),[B](#bua94lg-1-B),[missingflag](#mw%5F323e241b-f738-432a-a407-88a22bf9d13e)) also specifies how to treat missing values.

___ = min(___,"ComparisonMethod",[method](#mw%5Fc5ac18b5-d7f0-4187-a8e0-e65998cfc224)) optionally specifies how to compare elements for any of the previous syntaxes. For example, for a vector A = [-1 2 -9], the syntaxmin(A,[],"ComparisonMethod","abs") compares the elements of A according to their absolute values and returns a minimum value of -1.

Examples

collapse all

Create a vector and compute its smallest element.

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

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)

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

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

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

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.

Create a matrix containing NaN values.

A = [1.77 -0.005 3.98 -2.95; NaN 0.34 NaN 0.19]

A = 2×4

1.7700   -0.0050    3.9800   -2.9500
   NaN    0.3400       NaN    0.1900

Compute the minimum value of the matrix, including missing values. For matrix columns that contain any NaN value, the minimum is NaN.

M = min(A,[],"includemissing")

M = 1×4

   NaN   -0.0050       NaN   -2.9500

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

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

[M,I] = min(A,[],2,"linear")

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

Input Arguments

collapse all

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

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

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | categorical | datetime | duration | table | timetable
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.

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 an m-by-n input matrix,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. Thenmin(A,[],[1 2]) returns a 1-by-1-by-3 array whose elements are the minimums computed over each page ofA.

Mapping of a 2-by-3-by-3 input array to a 1-by-1-by-3 output array

Missing value condition, specified as one of the values in this table.

Value Input Data Type Description
"omitmissing" All supported data types Ignore missing values in the input arrays, and compute the minimum over fewer points. If all elements in the operating dimension are missing, then the corresponding element in M is missing.
"omitnan" double, single,duration
"omitnat" datetime
"omitundefined" categorical
"includemissing" All supported data types Include missing values in the input arrays when computing the minimum. If any element in the operating dimension is missing, then the corresponding element in M is missing.
"includenan" double, single,duration
"includenat" datetime
"includeundefined" categorical

Additional input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. 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 or1-by-N row vector). For more information, see Compatible Array Sizes for Basic Operations.

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

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

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

Output Arguments

collapse all

Minimum values, returned as a scalar, vector, matrix, multidimensional array or table.size(M,dim) is 1, while the sizes of all other dimensions match the size of the corresponding dimension inA, unless size(A,dim) is0. If size(A,dim) is0, then M is an empty array with the same size as A.

Index, returned as a scalar, vector, matrix, multidimensional array, or table. 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 ofA 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, multidimensional array, table, or timetable. The size ofC 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:

Extended Capabilities

expand all

Themin function supports tall arrays with the following usage notes and limitations:

For more information, see Tall Arrays.

Usage notes and limitations:

Usage notes and limitations:

Usage notes and limitations:

The min 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

expand all

Specifying a second input array as a character array returns an error. This change minimizes confusion with other arguments that can be specified as character vectors, such as the missing value condition. To maintain the previous functionality, you can convert the second input array to double, for example,min(A,double(B),"includenan").

Specifying a second input array as a character array gives a warning and will generate an error in a future release. This change minimizes confusion with other arguments that can be specified as character vectors, such as the missing value condition. To maintain the previous functionality, you can convert the second input array to double, for example,min(A,double(B),"includenan").

Omit or include all missing values in the input arrays when computing the minimum value by using the "omitmissing" or"includemissing" options. Previously,"omitnan", "includenan","omitnat", "includenat","omitundefined", and "includeundefined" specified a missing value condition that was specific to the data type of the input arrays.

The min function can calculate on all variables within a table or timetable without indexing to access those variables. All variables must have data types that support the calculation. For more information, see Direct Calculations on Tables and Timetables.

Specify the real or absolute value method for determining the minimum value of the input by using the ComparisonMethod parameter.

Operate on multiple dimensions of the input arrays at a time. Specify a vector of operating dimensions, or specify the "all" option to operate on all array dimensions.