Documentation

max

Largest elements in array

Syntax

M = max(A)
M = max(A,[],dim)
M = max(A,[],nanflag)
M = max(A,[],dim,nanflag)
[M,I] = max(___)
C = max(A,B)
C = max(A,B,nanflag)

Description

example

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

  • IfAis a vector, thenmax(A)returns the largest element ofA.

  • IfAis a matrix, thenmax(A)is a row vector containing the maximum value of each column.

  • IfAis a multidimensional array, thenmax(A)operates along the first array dimension whose size does not equal1, treating the elements as vectors. The size of this dimension becomes1while the sizes of all other dimensions remain the same. IfAis an empty array whose first dimension has zero length, thenmax(A)returns an empty array with the same size asA.

example

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

example

M= max(A,[],nanflag)specifies whether to include or omitNaNvalues in the calculation. For example,max(A,[],'includenan')includes allNaNvalues inAwhilemax(A,[],'omitnan')ignores them.

M= max(A,[],dim,nanflag)also specifies the dimension to operate along when using thenanflagoption.

example

[M,I] = max(___)finds the indices of the maximum values ofAand returns them in output vectorI, using any of the input arguments in the previous syntaxes. If the maximum value occurs more than once, thenmaxreturns the index corresponding to the first occurrence.

example

C= max(A,B)returns an array with the largest elements taken fromAorB.

C= max(A,B,nanflag)also specifies how to treatNaNvalues.

Examples

collapse all

Create a vector and compute its largest element.

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

Create a complex vector and compute its largest element, that is, the element with the largest magnitude.

A = [-2+2i 4+i -1-3i]; max(A)
ans = 4.0000 + 1.0000i

Create a matrix and compute the largest element in each column.

A = [2 8 4; 7 3 9]
A =2 8 4 7 3 9
M = max(A)
M =7 8 9

Create a matrix and compute the largest element in each row.

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
M = max(A,[],2)
M =1.7000 1.9900

Create a matrixAand compute the largest elements in each column, as well as the row indices ofAin which they appear.

A = [1 9 -2; 8 4 -5]
A =1 9 -2 8 4 -5
[M,I] = max(A)
M =8 9 -2
I =2 1 1

Create a matrix and return the largest value between each of its elements compared to a scalar.

A = [1 7 3; 6 2 9]
A =1 7 3 6 2 9
B = 5; C = max(A,B)
C =5 7 5 6 5 9

Create a matrixAand use its column representation,A(:), to find the value and index of the largest element.

A = [8 2 4; 7 3 9]
A =8 2 4 7 3 9
A(:)
ans =8 7 2 3 4 9
[M,I] = max(A(:))
M = 9
I = 6

Iis the index ofA(:)containing the largest element.

现在,使用ind2subfunction to extract the row and column indices ofAcorresponding to the largest element.

[I_row, I_col] = ind2sub(size(A),I)
I_row = 2
I_col = 3

If you need only the maximum value ofAand not its index, then call themaxfunction twice.

M = max(max(A))
M = 9

Create a vector and compute its maximum, excludingNaNvalues.

A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19]; M = max(A,[],'omitnan')
M = 3.9800

max(A)也会产生this result since'omitnan'is the default option.

Use the'includenan'flag to returnNaN.

M = max(A,[],'includenan')
M = NaN

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array.

  • IfAis complex, thenmax(A)returns the complex number with the largest magnitude. If magnitudes are equal, thenmax(A)returns the value with the largest magnitude and the largest phase angle.

  • IfAis a scalar, thenmax(A)returnsA.

  • IfAis a 0-by-0 empty array, thenmax(A)is as well.

IfAhas typecategorical, then it must be ordinal.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|categorical|datetime|duration
Complex Number Support:Yes

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

Dimensiondimindicates the dimension whose length reduces to1. Thesize(M,dim)is1, while the sizes of all other dimensions remain the same, unlesssize(A,dim)is0. Ifsize(A,dim)is0, thenmax(A,dim)returns an empty array with the same size asA.

Consider a two-dimensional input array,A:

  • Ifdim = 1, thenmax(A,[],1)returns a row vector containing the largest element in each column.

  • Ifdim = 2, thenmax(A,[],2)returns a column vector containing the largest element in each row.

maxreturnsAifdimis greater thanndims(A).

Additional input array, specified as a scalar, vector, matrix, or multidimensional array. Numeric inputsAandBmust either be the same size or have sizes that are compatible (for example,Ais anM-by-Nmatrix andBis a scalar or1-by-Nrow vector). For more information, seeCompatible Array Sizes for Basic Operations.

IfAandBdatetime、持续时间、或分类数组n they must be the same size unless one is a scalar.

  • AandBmust be the same data type unless one is adouble. In that case, the data type of the other array can besingle,duration, or any integer type.

  • IfAandBare ordinal categorical arrays, they must have the same sets of categories with the same order.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|categorical|datetime|duration
Complex Number Support:Yes

NaNcondition, specified as one of these values:

  • 'omitnan'— Ignore allNaNvalues in the input.

  • 'includenan'— Include theNaNvalues in the input for the calculation.

Fordatetimearrays, you can also use'omitnat'or'includenat'to omit and includeNaTvalues, respectively.

Themaxfunction does not support thenanflagoption forcategoricalarrays.

Data Types:char

Output Arguments

collapse all

Maximum values, returned as a scalar, vector, matrix, or multidimensional array.size(M,dim)is1, while the sizes of all other dimensions match the size of the corresponding dimension inA, unlesssize(A,dim)is0. Ifsize(A,dim)is0, thenMis an empty array with the same size asA.

Index to maximum values ofA, returned as a scalar, vector, matrix, or multidimensional array.Iis the same size asM. If the largest element occurs more than once, thenIcontains the index to the first occurrence of the value.

Maximum elements fromAorB, returned as a scalar, vector, matrix, or multidimensional array. The size ofCis determined by implicit expansion of the dimensions ofAandB. For more information, seeCompatible Array Sizes for Basic Operations.

The data type ofCdepends on the data types ofAandB:

  • IfAandBare the same data type, thenCmatches the data type ofAandB.

  • If eitherAorBissingle, thenCissingle.

  • If eitherAorBis an integer data type with the other a scalardouble, thenCassumes the integer data type.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?