MAX Maximum Function

Section: Elementary Functions

Usage

Computes the maximum of an array along a given dimension, or alternately, computes two arrays (entry-wise) and keeps the smaller value for each array. As a result, the 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.

Function Internals

In the general version of the 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

Example

The following piece of code demonstrates various uses of the maximum function. We start with the one-array version.
--> 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 = 
    8  127  -51  -57 
   -5  -25  -62  -14 
  -17  -73  127  -73 
 -102   21   36   39 

--> b = int8(100*randn(4))

b = 
   59  127   46   24 
  -98  -78  -50 -114 
 -128   57  127   -2 
  -64  -95   30  127 

--> max(a,b)

ans = 
  59 127  46  24 
  -5 -25 -50 -14 
 -17  57 127  -2 
 -64  21  36 127 

Or alternately, we can compare an array with a scalar

--> a = randn(2)

a = 
    1.4356    0.4894 
   -0.8816    0.6359 

--> max(a,0)

ans = 
    1.4356    0.4894 
         0    0.6359