Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

reordercats

Reorder categories in categorical array

Syntax

Description

example

B = reordercats(A) reorders the categories in the categorical array, A, to be in alphanumeric order.

The order of the categories is used by functions such as summary and histogram. If the categorical array is ordinal, the order of the categories defines their mathematical ordering. The first category specified is the smallest and the last category is the largest.

example

B = reordercats(A,neworder) puts the categories in the order specified by neworder.

Examples

collapse all

Create two categorical arrays, X and Y.

X = categorical({'frog';'cat';'cat';'ant';'frog'})

Y = categorical({'deer';'bear';'eagle';'deer'})
X = 

     frog 
     cat 
     cat 
     ant 
     frog 


Y = 

     deer 
     bear 
     eagle 
     deer 

X is a 5-by-1 categorical array. The categories of X are the sorted unique values from the array: {'ant';'cat';'frog'}.

Y is a 4-by-1 categorical array. The categories of Y are the sorted unique values from the array: {'bear';'deer';'eagle'}.

Concatenate X and Y into a single categorical array, A.

A = [X;Y]
A = 

     frog 
     cat 
     cat 
     ant 
     frog 
     deer 
     bear 
     eagle 
     deer 

vertcat appends the values from Y to the values from X.

List the categories of the categorical array, A.

acats = categories(A)
acats =

  6×1 cell array

    'ant'
    'cat'
    'frog'
    'bear'
    'deer'
    'eagle'

vertcat appends the categories of Y to the categories from X. The categories of A are not in alphabetical order.

Reorder the categories of A into alphabetical order.

B = reordercats(A)
B = 

     frog 
     cat 
     cat 
     ant 
     frog 
     deer 
     bear 
     eagle 
     deer 

The output categorical array, B, has the same elements in the same order as the input categorical array, A.

List the categories of the categorical array, B.

bcats = categories(B)
bcats =

  6×1 cell array

    'ant'
    'bear'
    'cat'
    'deer'
    'eagle'
    'frog'

The categories of B are in alphabetical order.

Create a categorical array containing the color of various items.

A = categorical({'red';'green';'blue';'red';'green';'red';'blue';'blue'})
A = 

     red 
     green 
     blue 
     red 
     green 
     red 
     blue 
     blue 

A is an 8-by-1 categorical array.

Display the categories of A.

categories(A)
ans =

  3×1 cell array

    'blue'
    'green'
    'red'

The categories of A are in alphabetical order and have no mathematical meaning.

Reorder the categories to match the order commonly used for colors.

B = reordercats(A,{'red','green','blue'})
B = 

     red 
     green 
     blue 
     red 
     green 
     red 
     blue 
     blue 

B contains the same values as A.

Display the categories of B.

categories(B)
ans =

  3×1 cell array

    'red'
    'green'
    'blue'

B is not ordinal and the order of the categories has no mathematical meaning. Although the categories appear in the order of the color spectrum, relational operations, such as greater than and less than, have no meaning.

Create an ordinal categorical array, A, containing modes of transportation. Order the categories based on the average price of travel.

A = categorical({'plane';'car'; 'train';'car';'plane';'car'},...
    {'car','train','plane'},'Ordinal',true)
A = 

     plane 
     car 
     train 
     car 
     plane 
     car 

A is a 6-by-1 ordinal categorical array.

Display the categories of A.

categories(A)
ans =

  3×1 cell array

    'car'
    'train'
    'plane'

Since A is ordinal, car < train < plane.

Reorder the categories to reflect a decrease in the cost of train travel.

B = reordercats(A,{'train','car','plane'})
B = 

     plane 
     car 
     train 
     car 
     plane 
     car 

B contains the same values as A.

Display the categories of B.

categories(B)
ans =

  3×1 cell array

    'train'
    'car'
    'plane'

The mathematical ordering of the categories is now train < car < plane. The results from relational operations, min, and max reflect the new category ordering.

Create a categorical array, A, containing modes of transportation.

A = categorical({'plane';'car';'train';'car';'car';'plane';'car'})
A = 

     plane 
     car 
     train 
     car 
     car 
     plane 
     car 

Display the categories of A.

categories(A)
ans =

  3×1 cell array

    'car'
    'plane'
    'train'

Reorder categories from least to most frequent occurrence in A.

B = countcats(A);
[C,neworder] = sort(B);
neworder
neworder =

     3
     2
     1

D = reordercats(A,neworder);
categories(D)
ans =

  3×1 cell array

    'train'
    'plane'
    'car'

Because countcats counts the occurrences of each category, neworder describes how to reorder the categories—not the elements—of A.

Input Arguments

collapse all

Categorical array, specified as a vector, matrix, or multidimensional array. If A is an ordinal categorical array, a reordering of the categories changes the mathematical meaning. Consequently, the relational operators, such as greater than and less than, might return different results.

New category order for B, specified as a cell array of character vectors or a numeric vector. neworder must be a permutation of categories(A).

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

Tips

  • To convert the categorical array, B, to an ordinal categorical array, use B = categorical(B,'Ordinal',true). You can specify the order of the categories with B = categorical(B,valueset,'Ordinal',true), where the order of the values in valueset defines the category order.

Introduced in R2013b


Was this topic helpful?