Section: Elementary Functions
max
function has a number of syntaxes. The first
one computes the maximum of an array along a given dimension.
The first general syntax for its use is either
[y,n] = max(x,[],d)
where x
is a multidimensional array of numerical type, in which case the
output y
is the maximum of x
along dimension d
.
The second argument n
is the index that results in the maximum.
In the event that multiple maxima are present with the same value,
the index of the first maximum is used.
The second general syntax for the use of the max
function is
[y,n] = max(x)
In this case, the maximum is taken along the first non-singleton
dimension of x
. For complex data types,
the maximum is based on the magnitude of the numbers. NaNs are
ignored in the calculations.
The third general syntax for the use of the max
function is as
a comparison function for pairs of arrays. Here, the general syntax is
y = max(x,z)
where x
and z
are either both numerical arrays of the same dimensions,
or one of the two is a scalar. In the first case, the output is the
same size as both arrays, and is defined elementwise by the smaller of the
two arrays. In the second case, the output is defined elementwise by the
smaller of the array entries and the scalar.
max
function which is applied to
a single array (using the max(x,[],d)
or max(x)
syntaxes),
The output is computed via
and the output array n
of indices is calculated via
In the two-array version (max(x,z)
), the single output is computed as
--> A = [5,1,3;3,2,1;0,3,1] A = 5 1 3 3 2 1 0 3 1
We first take the maximum along the columns, resulting in a row vector.
--> max(A) ans = 5 3 3
Next, we take the maximum along the rows, resulting in a column vector.
--> max(A,[],2) ans = 5 3 3
When the dimension argument is not supplied, max
acts along the first non-singular dimension. For a row vector, this is the column direction:
--> max([5,3,2,9]) ans = 9
For the two-argument version, we can compute the smaller of two arrays, as in this example:
--> a = int8(100*randn(4)) a = 0 115 15 -20 -26 127 1 -41 -12 5 -84 52 85 -108 -7 -100 --> b = int8(100*randn(4)) b = -30 14 -33 -69 -62 -71 48 8 -52 2 -95 75 40 44 120 -4 --> max(a,b) ans = 0 115 15 -20 -26 127 48 8 -12 5 -84 75 85 44 120 -4
Or alternately, we can compare an array with a scalar
--> a = randn(2) a = 2.2822 -0.9318 -0.3667 0.5529 --> max(a,0) ans = 2.2822 0 0 0.5529