Documentation

maxk

Findklargest elements of array

Syntax

B = maxk(A,k)
B = maxk(A,k,dim)
B = maxk(___,'ComparisonMethod',c)
[B,I] = maxk(___)

Description

example

B= maxk(A,k)returns theklargest elements ofA.

  • IfAis a vector, thenmaxkreturns a vector containing theklargest elements ofA.

  • IfAis a matrix, thenmaxkreturns a matrix whose columns contain theklargest elements of each column ofA.

  • IfAis a multidimensional array, thenmaxkreturns theklargest elements along the first dimension whose size does not equal 1.

example

B= maxk(A,k,dim)determines theklargest elements ofAalong dimensiondim.

example

B= maxk(___,'ComparisonMethod',c)optionally specifies how to compare elements ofAfor any of the previous syntaxes. For example,maxk(A,k,'ComparisonMethod','abs')returns theklargest elements ofAaccording to their absolute values.

example

[B,I] = maxk(___)finds the indices of the largestkvalues ofAand returns them inI.

Examples

collapse all

Compute the largest 3 elements of a vector.

A = 1:10; B = maxk(A,3)
B =1×310 9 8

Compute the largest 3 elements of each row of a matrix.

A = magic(5)
A =5×517 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
B = maxk(A,3,2)
B =5×324 17 15 23 16 14 22 20 13 21 19 12 25 18 11

Compute the 2 largest elements of a complex vector according to their magnitude, and return the indices where they are located in the input vector.

A = [2-2i 5+i -7-3i -1+i]
A =1×4 complex2.0000 - 2.0000i 5.0000 + 1.0000i -7.0000 - 3.0000i -1.0000 + 1.0000i ⋯
[B,I] = maxk(A,2,'ComparisonMethod','abs')
B =1×2 complex-7.0000 - 3.0000i 5.0000 + 1.0000i
I =1×23 2

Input Arguments

collapse all

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

  • IfAis a vector, thenmaxkreturns a vector containing theklargest elements ofA.

  • IfAis a matrix, thenmaxkreturns a matrix whose columns contain theklargest elements of each column ofA.

  • IfAis a multidimensional array, thenmaxkreturns theklargest elements along the first dimension whose size does not equal 1.

IfAhas typecategorical, then it must be ordinal.

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

Number of maxima to return, specified as a positive integer scalar. Ifkis greater than or equal to the number of elements in the operating dimension, thenmaxksorts the input array along that dimension.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Operating dimension, specified as a positive integer scalar. By default,maxkoperates along the first dimension whose size does not equal 1.

For example, ifAis a matrix, thenmaxk(A,k,1)operates along the rows ofA, computing maximums for each column.

maxk(A,k,2)operates along the columns ofA, computing maximums for each row.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Comparison method, specified as one of the following:

  • 'auto'— Compare elements of inputAbyreal(A)whenAis real, and byabs(A)whenAis complex.

  • 'real'— Compare elements of inputAbyreal(A)whenAis real or complex. IfAhas elements with equal real parts, then useimag(A)to break ties.

  • 'abs'— Compare elements of inputAbyabs(A)whenAis real or complex. IfAhas elements with equal magnitude, then useangle(A)in the interval (-π,π] to break ties.

Output Arguments

collapse all

Output array, returned as a scalar, vector, matrix, or multidimensional array.maxkreturns thekelements in order from largest to smallest. The order of the elements inBpreserves the order of any equal elements inA.

Index array, returned as a vector, matrix, or multidimensional array.Iis the same size asB. If the output arrayBcontains repeated elements, then the order of their indices inImatches the order in which they appear in the input array.

Extended Capabilities

See Also

||

Introduced in R2017b

Was this topic helpful?