min
minimum
Syntax
m = min(A) Col = min(A, 'c') Row = min(A, 'r''m') M = min(A1, A2,..., An) M = min(list(A1, A2,..., An)) [.., K] = min(..)
Arguments
 A, A1, ..., An
scalars, vectors, matrices or hypermatrices of encoded integers or of real numbers, in dense or sparse format. They must have the same sizes, or be mixed with scalars (scalars are then implicitly expanded to the arrays sizes). Sparse arrays can't be mixed with dense ones, except with dense scalars.
 m
single number = minimum of all values of
A
elements. Always in dense format, even whenA
is sparse encoded. Col
column vector if
A
is a 2D matrix, or hypermatrix of size(A) with size(A,2) set to 1: Minima over columns (for each row). IfA
is sparse, thenCol
is sparse as well. Row
row vector if
A
is a 2D matrix, or hypermatrix of size(A) with size(A,1) set to 1: Minima over rows (for each column). IfA
is sparse, thenRow
is sparse as well. M
Array of size =
size(A1)
, such that for any qM(q) = min(A1(q),A2(q),..An(q))
IfA
,..,An
are sparse, thenM
is sparse as well. K
Indices in
A
.. of the (first) minimum found. When[m,K]=min(A)
is used, If
A
is a vector, K is a scalar.  Otherwise,
K
is a row vector [i,j,..] of subscripts.
For other syntaxes,
K
has the shape and sizes ofCol
,Row
, andM
.With the
[M,K] = min(A1,A2,..,An)
syntax, we have, for any linear index q:[M(q),K(q)] = min([A1(q) A2(q) .. An(q)])
.K
is always in dense format, even whenA, A1,..,An
are sparseencoded. Hence, when the[M,K]=min(A1,A2,..)
syntax is used with huge but sparse matrices, this may lead to a huge denseK
matrix. The user must check that enough memory is available for it. If
Description
For A
, a real vector or matrix, min(A)
is the
least element of A
.
[m,K]=min(A)
gives in addition the indices of the first minimum.
A second argument of type string 'r'
or
'c'
can be used : 'r'
is used to get
a row vector Row
such that Row(j)
contains the minimum of the j
th column A(:,j)
,
K(j)
gives the index of the row
which contains the minimum, for the column #j
.
'c'
is used for the dual operation on the rows of
A
. 'm'
is used for compatibility with Matlab.
[M,K]=min(list(A1,...,An))
is an equivalent
syntax of [M,K]=min(A1,A2,...,An)
.

If min(A1, A2,..., An) is used with a huge input sparse matrix of
low density, together with a strictly negative scalar input, the sparse result will no longer
have any 0 value: It will be a sparse array with density=1, that may lead to a memory failure. 
Examples
[m, k] = min([]) [m, k] = min([5 3 ; 2 %nan]) [m, n] = min([5 3 ; 2 %nan], 4) [m, k] = min([5 1 2], [1 5 1], [0 1 3]) [m, k] = min(list([5 1 2], [1 5 1], [0 1 3]))
> [m, k] = min([]) m = [] k = [] > [m, k] = min([5 3 ; 2 %nan]) m = 2. k = 2. 1. > [m, k] = min([5 3 ; 2 %nan], 4) m = 4. 3. 2. 4. k = 2. 1. 1. 2. > [m, k] = min([5 1 2], [1 5 1], [0 1 3]) m = 0. 1. 1. k = 3. 1. 2.
With the "r" or "c" options:
A = grand(4,6,"uin",0,30); A(3,4) = %nan [Row, K] = min(A, "r") [Col, K] = min(A, "c")
> A = grand(4,6,"uin",0,30); A(3,4) = %nan A = 24. 14. 24. 4. 6. 11. 23. 25. 29. 6. 19. 5. 30. 2. 20. Nan 6. 6. 20. 8. 13. 14. 16. 3. > [Row, K] = min(A, "r") Row = 20. 2. 13. 4. 6. 3. K = 4. 3. 4. 1. 1. 4. > [Col, K] = min(A, "c") Col = 4. 5. 2. 3. K = 4. 6. 2. 6.
With sparse inputs:
s = sprand(5,4,0.5); k = s~=0; s(k) = round((s(k)0.5)*10), full(s) [Row, K] = min(s, "r") [Col, K] = min(s, "c") [M, K] = min(s,1); [full(s) ones(s(:,1))*%nan full(M)] issparse(M) K
> s = sprand(5,4,0.5); k = s~=0; s(k) = round((s(k)0.5)*10), full(s) s = ( 5, 4) sparse matrix ( 1, 3) 5. ( 1, 4) 2. ( 2, 1) 3. ( 2, 3) 5. ( 3, 1) 3. ( 3, 2) 1. ( 3, 3) 3. ( 3, 4) 4. ( 5, 3) 4. ( 5, 4) 5. ans = 0. 0. 5. 2. 3. 0. 5. 0. 3. 1. 3. 4. 0. 0. 0. 0. 0. 0. 4. 5. > [Row, K] = min(s, "r") Row = ( 1, 4) sparse matrix ( 1, 1) 3. ( 1, 2) 1. ( 1, 3) 5. ( 1, 4) 5. K = 2. 3. 2. 5. > [Col, K] = min(s, "c") Col = ( 5, 1) sparse matrix ( 1, 1) 2. ( 2, 1) 5. ( 3, 1) 1. ( 5, 1) 5. K = 4. 3. 2. 1. 4. > [M, K] = min(s,1); [full(s) ones(s(:,1))*%nan full(M)] ans = 0. 0. 5. 2. Nan 0. 0. 1. 2. 3. 0. 5. 0. Nan 3. 0. 5. 0. 3. 1. 3. 4. Nan 1. 1. 1. 1. 0. 0. 0. 0. Nan 0. 0. 0. 0. 0. 0. 4. 5. Nan 0. 0. 1. 5. > issparse(M) ans = 1. > K K = 1. 1. 2. 1. 1. 1. 1. 1. 2. 1. 2. 2. 1. 1. 1. 1. 1. 1. 2. 1.
See also
History
Version  Description 
6.0.2  min() now actually works with sparse matrices 
Report an issue  
<< max  Matrix operations  norm >> 