categorical

Methods

Method Reference: categorical.unique

categorical: B = unique (A)
categorical: B = unique (A, setOrder)
categorical: B = unique (A, occurrence)
categorical: B = unique (A, setOrder, occurrence)
categorical: B = unique (A, occurrence, setOrder)
categorical: B = unique (A, …, 'rows')
categorical: [B, ixA, ixB] = unique (…)

Unique values in a categorical array.

B = unique (A) returns the unique values of the categorical array A in the categorical vector B sorted according to the order of categories in A. B retains the same categories as A. If A is a row vector, then B is also a row vector, otherwise unique returns a column vector.

B = unique (A, setOrder) returns the unique values of the categorical array A in an order as specified by setOrder, which can be either of the following values:

  • 'sorted' (default) returns the unique values sorted in ascending order.
  • 'stable' returns the unique values according to their order of occurrence.

B = unique (A, occurrence) returns the unique values of the categorical array A according to their order of occurrence. occurrence can be either of the following values:

  • 'first' (default) returns the first occurrence of each unique value, i.e. the lowest possible indices are returned.
  • 'last' returns the last occurrence of each unique value, i.e. the highest possible indices are returned.

You can specify setOrder and occurrence arguments together.

B = unique (A, …, 'rows') returns the unique rows of A by treating each row as a single entity. The 'rows' option can be used alone or in any combination with the setOrder and occurrence arguments. 'rows' can be placed at any position in the function’s argument list after the input array A. However, this syntax is only valid for 2-dimensional categorical arrays.

[B, ixA, ixB] = unique (…) also returns index vectors ixA and ixB using any of the previous syntaxes. ixA and ixB map the arrays A and B to one another such that B = A(ixA) and A = B(ixB). When the 'rows' optional argument is specified, then B = A(ixA,:) and A = B(ixB,:).

Source Code: categorical

Example: 1

unique returns the distinct values present in the array, as a categorical array. (Contrast categories, which lists the full declared label set even when some categories are unused.)

 C = categorical ({'M'; 'S'; 'M'; 'L'; 'S'})
C =
  5x1 categorical array

    M    
    S    
    M    
    L    
    S
 unique (C)
ans =
  3x1 categorical array

    L    
    M    
    S