Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.1.1 - English

Change language to:
Français - 日本語 - Português - Русский

Please note that the recommended version of Scilab is 2024.0.0. This page might be outdated.
See the recommended documentation of this function

Scilab Help >> Elementary Functions > Matrix operations > max

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 when A 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). If A is sparse, then Col 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). If A is sparse, then Row is sparse as well.

M

Array of size = size(A1), such that for any q M(q) = max(A1(q),A2(q),..An(q)) If A,..,An are sparse, then M 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 of Col, Row, and M.

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 when A, 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 dense K matrix. The user must check that enough memory is available for it.

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

  • max() ignores NaN values (unless there are only NaN values).
  • max([]) returns [] for values and K.
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

  • min — minimum
  • strange — range
  • mean — mean of all values, or means along a given dimension
  • gsort — sorts boolean, numerical and string arrays
  • find — gives the indices of %T or non-zero elements
  • full — sparse to full matrix conversion

History

VersionDescription
6.0.2 max() now actually works with sparse matrices
Report an issue
<< kron .\. ./. Matrix operations min >>

Copyright (c) 2022-2023 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Jan 03 14:23:23 CET 2022