max

Largest elements in array

Syntax

Description

example

M = max(A) returns the largest elements of A.

  • If A is a vector, then max(A) returns the largest element of A.

  • If A is a matrix, then max(A) treats the columns of A as vectors and returns a row vector of largest elements.

  • If A is a multidimensional array, then max(A) treats the values along the first array dimension whose size does not equal 1 as vectors and returns an array of maximum values. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same.

example

M = max(A,[],dim) returns the largest elements along dimension dim. For example, if A is a matrix, then max(A,[],2) is a column vector containing the maximum value of each row.

example

[M,I] = max(___) finds the indices of the maximum values of A and returns them in output vector I, using any of the input arguments in the previous syntaxes. If there are several identical maximum values, then max returns the index of the first one.

example

C = max(A,B) returns an array the same size as A and B with the largest elements taken from A or B. The dimensions of A and B must match, or one can be a scalar.

Examples

expand all

Largest Element in Vector

Return the maximum value from a vector.

A = [23 42 37 18 52];
M = max(A)
M =

    52

Single Largest Element in Array

Create a 2-by-3 matrix.

A = [4 8 2; 9 3 7]
A =

     4     8     2
     9     3     7

To find the single largest element in a matrix, you can apply the max function twice, max(max(A)). However, this does not return the index to the largest element.

Use A(:) to find the maximum element in the column representation of the elements from the array A. Also return the index to the maximum element.

[M,I] = max(A(:))
M =

     9


I =

     2

A(:) is a column vector composed of all the columns from the matrix, where each column is appended to the last. A(2) returns the maximum element, 9.

Maximum Value from Each Matrix Column

Create a 2-by-3 matrix.

A = [2 8 4; 7 3 9]
A =

     2     8     4
     7     3     9

Find the maximum of each column.

[M,I] = max(A)
M =

     7     8     9


I =

     2     1     2

A(I(n),n) contains the maximum element in the nth column of A.

Maximum Value from Each Matrix Row

Create a 2-by-3 matrix.

A = [1.7 1.2 1.5; 1.3 1.6 1.99]
A =

    1.7000    1.2000    1.5000
    1.3000    1.6000    1.9900

Find the maximum of each row.

[M,I] = max(A,[],2)
M =

    1.7000
    1.9900


I =

     1
     3

A(n,I(n)) contains the maximum element in the nth row of A.

Maximum Elements from Two Arrays

Create two arrays of the same size.

A = [1 8 3; 7 2 9]
B = [2 6 4; 5 2 7]
A =

     1     8     3
     7     2     9


B =

     2     6     4
     5     2     7

Find the maximum elements from A or B.

C = max(A,B)
C =

     2     8     4
     7     2     9

C contains the largest elements from each pair of corresponding elements in A and B.

Maximum Elements from Array and Scalar

Compare each element of matrix A to a scalar and return the maximum.

Create a 2-by-3 matrix.

A = [1 7 3; 6 2 9]
A =

     1     7     3
     6     2     9

Compare each element of A to the scalar value 5.

B = 5;
C = max(A,B)
C =

     5     7     5
     6     5     9

C contains the larger value between each element of A and the scalar 5.

Input Arguments

expand all

A — Input arrayscalar | vector | matrix | multidimensional array

Input array, specified as a scalar, vector, matrix, or multidimensional array. A can be a numeric array, logical array, ordinal categorical array, datetime array, or duration array.

Complex Number Support: Yes

dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, the default is the first array dimension whose size does not equal 1.

Dimension dim indicates the dimension whose length reduces to 1. The size(M,dim) is 1, while the sizes of all other dimensions remain the same, unless size(A,dim) is 0. If size(A,dim) is 0, then max returns an empty array with the same dimension sizes.

Consider a two-dimensional input array, A:

  • If dim = 1, then max(A,[],1) returns a row vector containing the largest element in each column.

  • If dim = 2, then max(A,[],2) returns a column vector containing the largest element in each row.

max returns A if dim is greater than ndims(A).

B — Second input arrayscalar | vector | matrix | multidimensional array

Second input array, specified as a scalar, vector, matrix, or multidimensional array. B can be a numeric array, logical array, ordinal categorical array, datetime array, or duration array.

The dimensions of A and B must match, or one can be a scalar.

If A and B are ordinal categorical arrays, they must have the same sets of categories, including their order.

A and B can be the same data type or one can be double and combine with single , any integer data type, or duration.

Complex Number Support: Yes

Output Arguments

expand all

M — Maximum valuesscalar | vector | matrix | multidimensional array

Maximum values, returned as scalar, vector, matrix, or multidimensional array.

The size(M,dim) is 1, while the sizes of all other dimensions match the size of the corresponding dimension in A, unless size(A,dim) is 0. If size(A,dim) is 0, then M is an empty array with the same dimension sizes as A.

I — Index to Aarray the same size as A

Index to A, returned as an array the same size as A. If the largest element occurs more than once, then I contains the index to the first occurrence of the value.

C — Maximum elements from A or Barray

Maximum elements from A or B, returned as an array.

The size of C is as follows:

  • If A and B are arrays of the same size, then the size of C matches the size of A and B.

  • If either A or B is a scalar, then the size of C matches the size of the nonscalar input array.

The data type of C is as follows:

  • If A and B are the same data type, then C matches the data type of A and B.

  • If either A or B is single, then C is single.

  • If A and B are an integer data type and a scalar double, then C is the same integer data type.

More About

expand all

Algorithms

The max function ignores NaNs. When all elements in A are NaNs, then max returns the first one as the maximum. Undefined values in categorical arrays and NaT values in datetime arrays are similar to NaNs in numeric arrays.

When A is complex, the max function returns the complex number with the largest complex modulus (magnitude). If magnitudes are equal, then max returns the value with the largest magnitude and the largest phase angle.

When you use max(A,B) with an empty array and a scalar, then max returns an empty array.

See Also

| | | |

Was this topic helpful?