1 <?xml version="1.0" encoding="UTF-8"?>
3 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) 2008 - INRIA
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
6 * Copyright (C) 2018 - Samuel GOUGEON
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
16 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
17 xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml"
18 xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
19 xmlns:scilab="http://www.scilab.org" xml:id="max" xml:lang="en">
21 <refname>max</refname>
22 <refpurpose>maximum</refpurpose>
30 M = max(A1, A2,..., An)
31 M = max(list(A1, A2,..., An))
36 <title>Arguments</title>
39 <term>A, A1, ..., An</term>
42 scalars, vectors, matrices or hypermatrices of encoded integers or of real
43 numbers, in dense or sparse format. They must have the same sizes, or be
44 mixed with scalars (scalars are then implicitly expanded to the arrays sizes).
45 Sparse arrays can't be mixed with dense ones, except with dense
46 <emphasis>negative</emphasis> scalars.
54 single number = maximum of all values of <varname>A</varname> elements.
55 Always in dense format, even when <varname>A</varname> is sparse encoded.
63 column vector if <varname>A</varname> is a 2D matrix, or hypermatrix of
64 size(A) with size(A,2) set to 1: Maxima over columns (for each row).
65 If <varname>A</varname> is sparse, then <varname>Col</varname> is sparse
74 row vector if <varname>A</varname> is a 2D matrix, or hypermatrix of
75 size(A) with size(A,1) set to 1: Maxima over rows (for each column).
76 If <varname>A</varname> is sparse, then <varname>Row</varname> is sparse
85 Array of size = <literal>size(A1)</literal>, such that for any q
86 <literal>M(q) = max(A1(q),A2(q),..An(q))</literal>
87 If <varname>A</varname>,..,<varname>An</varname> are sparse, then
88 <varname>M</varname> is sparse as well.
96 Indices in <varname>A</varname>.. of the (first) maximum found.
97 When <literal>[m,K]=max(A)</literal> is used,
100 If <literal>A</literal> is a vector, K is a scalar.
103 Otherwise, <varname>K</varname> is a row vector [i,j,..] of subscripts.
108 For other syntaxes, <varname>K</varname> has the shape and sizes of
109 <varname>Col</varname>, <varname>Row</varname>, and <varname>M</varname>.
112 With the <literal>[M,K] = max(A1,A2,..,An)</literal> syntax, we have,
113 for any linear index q:
114 <literal>[M(q),K(q)] = max([A1(q) A2(q) .. An(q)])</literal>.
118 <varname>K</varname> is always in dense format, even when
119 <varname>A, A1,..,An</varname> are sparse-encoded. Hence, when the
120 <literal>[M,K]=max(A1,A2,..)</literal> syntax is used with huge but
121 sparse matrices, this may lead to a huge <emphasis>dense</emphasis>
122 <varname>K</varname> matrix. The user must check that enough memory
131 <title>Description</title>
133 For <literal>A</literal>, a real vector or matrix, <literal>max(A)</literal> is the
134 greatest element of <literal>A</literal>.
137 <literal>[m,K]=max(A)</literal> gives in addition the indices of the first maximum.
140 A second argument of type string <literal>'r'</literal> or
141 <literal>'c'</literal> can be used : <literal>'r'</literal> is used to get
142 a row vector <literal>Row</literal> such that <literal>Row(j)</literal>
143 contains the maximum of the <literal>j</literal>th column <literal>A(:,j)</literal>,
144 <literal>K(j)</literal> gives the index of the row
145 which contains the maximum, for the column #<literal>j</literal>.
148 <literal>'c'</literal> is used for the dual operation on the rows of
149 <literal>A</literal>. <literal>'m'</literal> is used for compatibility with Matlab.
152 <literal>[M,K]=max(list(A1,...,An))</literal> is an equivalent
153 syntax of <literal>[M,K]=max(A1,A2,...,An)</literal>.
158 max() ignores NaN values (unless there are only NaN values).
161 <literal>max([])</literal> returns
162 <literal>[]</literal> for values and <varname>K</varname>.
168 <title>Examples</title>
169 <programlisting role="example"><![CDATA[
171 [m, k] = max([1 5 ; 3 %nan])
172 [m, k] = max([1 5 ; 3 %nan], 2.5)
173 [m, k] = max([5 -1 1], [1 0 3], [2 1 3])
174 [m, k] = max(list([5 -1 1], [1 0 3], [2 1 3]))
183 --> [m, k] = max([1 5 ; 3 %nan])
190 --> [m, k] = max([1 5 ; 3 %nan], 2.5)
199 --> [m, k] = max([5 -1 1], [1 0 3], [2 1 3])
207 <emphasis role="bold">With the "r" or "c" options</emphasis>:
209 <programlisting role="example"><![CDATA[
210 A = grand(4,6,"uin",0,30); A(3,4) = %nan
211 [Row, K] = max(A, "r")
212 [Col, K] = max(A, "c")
215 --> A = grand(4,6,"uin",0,30); A(3,4) = %nan
217 18. 3. 22. 0. 13. 28.
218 16. 20. 25. 6. 10. 1.
219 25. 26. 20. Nan 2. 21.
220 5. 9. 16. 15. 24. 25.
222 --> [Row, K] = max(A, "r")
224 25. 26. 25. 15. 24. 28.
229 --> [Col, K] = max(A, "c")
243 <emphasis role="bold">With sparse inputs</emphasis>:
245 <programlisting role="example"><![CDATA[
246 s = sprand(5,4,0.5); k = s~=0; s(k) = round((s(k)-0.5)*10), full(s)
247 [Row, K] = max(s, "r")
248 [Col, K] = max(s, "c")
249 [M, K] = max(s, -1); [full(s) ones(s(:,1))*%nan full(M)]
254 --> s = sprand(5,4,0.5); k = s~=0; s(k) = round((s(k)-0.5)*10), full(s)
256 ( 5, 4) sparse matrix
277 --> [Row, K] = max(s, "r")
279 ( 1, 4) sparse matrix
287 --> [Col, K] = max(s, "c")
289 ( 5, 1) sparse matrix
302 --> [M, K] = max(s, -1); [full(s) ones(s(:,1))*%nan full(M)]
304 0. -2. -4. 3. Nan 0. -1. -1. 3.
305 -5. 0. 0. 3. Nan -1. 0. 0. 3.
306 0. -2. -4. 0. Nan 0. -1. -1. 0.
307 0. 4. 0. 2. Nan 0. 4. 0. 2.
308 0. -5. 5. -4. Nan 0. -1. 5. -1.
323 <refsection role="see also">
324 <title>See also</title>
325 <simplelist type="inline">
327 <link linkend="min">min</link>
330 <link linkend="strange">strange</link>
333 <link linkend="mean">mean</link>
336 <link linkend="gsort">gsort</link>
339 <link linkend="find">find</link>
342 <link linkend="full">full</link>
346 <refsection role="history">
347 <title>History</title>
350 <revnumber>6.0.2</revnumber>
352 max() now actually works with sparse matrices