categorical

Methods

Method Reference: categorical.topkrows

categorical: B = topkrows (A, K)
categorical: B = topkrows (A, K, col)
categorical: B = topkrows (A, K, direction)
categorical: B = topkrows (A, K, col, direction)

Top K sorted rows of categorical array.

B = topkrows (A, K) returns the top K rows of the 2-D categorical array A sorted in descending order as a group. K must be a nonnegative integer scalar. If K is larger than the number of rows in A, then all of them are returned. B keeps the categories of A, including their order.

Missing elements (<undefined>) are not ranked. Within each sort column they are placed after the elements that are defined, whichever direction is asked for, so a row is demoted only by a missing element in a column that actually decides its position. Rows comparing as equal keep their original order.

B = topkrows (A, K, col) returns the top K rows of the 2-D categorical array A sorted according to the columns specified by the numeric vector col, which must contain positive integers indexing existing columns in A. Columns are used as sort keys in the order given, and those not listed are not used at all. The direction is descending unless direction says otherwise.

B = topkrows (A, K, direction) returns the top K rows of the 2-D categorical array A sorted according to direction, which can be either 'descend' (default) or 'ascend' applying to all columns in A. Alternatively, direction can be a cell array of character vectors specifying the sorting direction for each individual column of A, in which case the number of elements in direction must equal the number of columns in A.

B = topkrows (A, K, col, direction) returns the top K rows of the 2-D categorical array A sorted according to the columns specified in col using the corresponding sorting direction specified in direction. col and direction must have the same length, but not necessarily the same number of elements as the columns in A.

B = topkrows (…, 'MissingPlacement', MP) specifies where the missing elements (<undefined>) are placed within each sort column, with any of the following options specified in MP:

  • 'last', which is the default, places missing elements last whichever direction is asked for.
  • 'first' places missing elements first.
  • 'auto' places missing elements last for an ascending sort and first for a descending one, as sortrows does.

This is an Octave extension: MATLAB has no such option here and always ranks as 'last' does.

[B, index] = topkrows (A, …) also returns an index vector containing the original row indices of A in B, such that B = A(index,:).

Source Code: categorical