topkrows
Top rows in sorted order
Syntax
Description
returns the first B
= topkrows(X
,k
)k
rows in X
. 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(___,direction
)direction
can be 'ascend'
,
'descend'
, or a cell array containing those
values.
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.
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.