categorical

Methods

Method Reference: categorical.reordercats

categorical: B = reordercats (A)
categorical: B = reordercats (A, neworder)

Reorder categories in categorical array.

B = reordercats (A) reorders the categories of A in alphanumeric order.

B = reordercats (A, neworder) reorders the categories of A according to the order specified by neworder, which must define a permutation of categories (A).

neworder can be a numeric vector, a string array, a cell array of character vectors, or any type of array that can be converted to a cell array of character vectors with the cellstr function as long as it indexes all existing categories in A.

Source Code: categorical

Example: 1

reordercats changes the order of the categories. For an ordinal array this redefines the ranking used by <, >, sort, min and max.

 C = categorical ({'M'; 'S'; 'L'}, {'S', 'M', 'L'}, 'Ordinal', true)
C =
  3x1 categorical array

    M    
    S    
    L
 categories (C)
ans =
  3x1 cell array

    {'S'}    
    {'M'}    
    {'L'}

Reverse the order so that S now ranks highest.

 B = reordercats (C, {'L', 'M', 'S'});
 categories (B)
ans =
  3x1 cell array

    {'L'}    
    {'M'}    
    {'S'}

The comparison follows the new ranking.

 B >= 'M'
ans =

  1
  1
  0