max
maximum
Syntax
m = max(A) Col = max(A, 'c') Row = max(A, 'r'|'m') M = max(A1, A2,..., An) M = max(list(A1, A2,..., An)) [.., K] = max(..)
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 = maximum 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: Maxima 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: Maxima 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) = max(A1(q),A2(q),..An(q))
IfA
,..,An
are sparse, thenM
is sparse as well.- K
Indices in
A
.. of the (first) maximum found. When[m,K]=max(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] = max(A1,A2,..,An)
syntax, we have, for any linear index q:[M(q),K(q)] = max([A1(q) A2(q) .. An(q)])
.K
is always in dense format, even whenA, A1,..,An
are sparse-encoded. Hence, when the[M,K]=max(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, max(A)
is the
greatest element of A
.
[m,K]=max(A)
gives in addition the indices of the first maximum.
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 maximum of the j
th column A(:,j)
,
K(j)
gives the index of the row
which contains the maximum, 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]=max(list(A1,...,An))
is an equivalent
syntax of [M,K]=max(A1,A2,...,An)
.
|
If max(A1, A2,..., An) is used with a huge input sparse matrix of
low density, together with a strictly positive 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] = max([]) [m, k] = max([1 5 ; 3 %nan]) [m, k] = max([1 5 ; 3 %nan], 2.5) [m, k] = max([5 -1 1], [1 0 3], [2 1 3]) [m, k] = max(list([5 -1 1], [1 0 3], [2 1 3]))
--> [m, k] = max([]) m = [] k = [] --> [m, k] = max([1 5 ; 3 %nan]) m = 5. k = 1. 2. --> [m, k] = max([1 5 ; 3 %nan], 2.5) m = 2.5 5. 3. 2.5 k = 2. 1. 1. 2. --> [m, k] = max([5 -1 1], [1 0 3], [2 1 3]) m = 5. 1. 3. k = 1. 3. 2.
With the "r" or "c" options:
A = grand(4,6,"uin",0,30); A(3,4) = %nan [Row, K] = max(A, "r") [Col, K] = max(A, "c")
--> A = grand(4,6,"uin",0,30); A(3,4) = %nan A = 18. 3. 22. 0. 13. 28. 16. 20. 25. 6. 10. 1. 25. 26. 20. Nan 2. 21. 5. 9. 16. 15. 24. 25. --> [Row, K] = max(A, "r") Row = 25. 26. 25. 15. 24. 28. K = 3. 3. 2. 4. 4. 1. --> [Col, K] = max(A, "c") Col = 28. 25. 26. 25. K = 6. 3. 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] = max(s, "r") [Col, K] = max(s, "c") [M, K] = max(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, 2) -2. ( 1, 3) -4. ( 1, 4) 3. ( 2, 1) -5. ( 2, 4) 3. ( 3, 2) -2. ( 3, 3) -4. ( 4, 2) 4. ( 4, 4) 2. ( 5, 2) -5. ( 5, 3) 5. ( 5, 4) -4. ans = 0. -2. -4. 3. -5. 0. 0. 3. 0. -2. -4. 0. 0. 4. 0. 2. 0. -5. 5. -4. --> [Row, K] = max(s, "r") Row = ( 1, 4) sparse matrix ( 1, 2) 4. ( 1, 3) 5. ( 1, 4) 3. K = 1. 4. 5. 1. --> [Col, K] = max(s, "c") Col = ( 5, 1) sparse matrix ( 1, 1) 3. ( 2, 1) 3. ( 4, 1) 4. ( 5, 1) 5. K = 4. 4. 1. 2. 3. --> [M, K] = max(s, -1); [full(s) ones(s(:,1))*%nan full(M)] ans = 0. -2. -4. 3. Nan 0. -1. -1. 3. -5. 0. 0. 3. Nan -1. 0. 0. 3. 0. -2. -4. 0. Nan 0. -1. -1. 0. 0. 4. 0. 2. Nan 0. 4. 0. 2. 0. -5. 5. -4. Nan 0. -1. 5. -1. --> issparse(M) ans = 1. --> K K = 1. 2. 2. 1. 2. 1. 1. 1. 1. 2. 2. 1. 1. 1. 1. 1. 1. 2. 1. 2.
See also
History
Version | Description |
6.0.2 | max() now actually works with sparse matrices |
Report an issue | ||
<< kron .\. ./. | Opérations matricielles | min >> |