topkrows
Top rows in sorted order
Syntax
Description
Array Data
sorts the rows in B
= topkrows(X
,k
)X
and returns the top k
rows of the sorted data. The rows are sorted in descending order (for numeric
data) or reverse alphabetical order (for text data).
topkrows
sorts based on the elements in the first
column. When the first column contains elements of equal value,
topkrows
sorts according to the elements in the next
column and repeats this behavior for succeeding equal values.
sorts the results by the columns specified by B
= topkrows(X
,k
,col
)col
. Use this
syntax to perform multiple column sorts in succession. For example,
topkrows(X,k,5)
sorts the rows of X
in
descending order based on the elements in the fifth column.
topkrows(X,k,[4 6])
first sorts the rows in descending
order by the elements in the fourth column, and then it sorts based on the
elements in the sixth column to break ties.
specifies the direction of the sorting using any of the previous
syntaxes.B
= topkrows(X
,___,direction
)
For example, topkrows(A,2,[2 3],{'ascend' 'descend'})
gets
the top 2 rows by first sorting rows in ascending order by the elements in
column 2. Then, it sorts the rows with equal entries in column 2 in descending
order by the elements in column 3.
Table Data
Examples
Input Arguments
Output Arguments
Tips
topkrows
does not do a full sort of the input data, so it is generally faster thansort
andsortrows
when the number of requested rows is small.