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.

mergecats

Merge categories in categorical array

Syntax

  • B = mergecats(A,oldcats)
    example
  • B = mergecats(A,oldcats,newcat)
    example

Description

example

B = mergecats(A,oldcats) merges two or more categories in A into the first category, oldcats(1). Any values in A from oldcats become oldcats(1) in B.

example

B = mergecats(A,oldcats,newcat) merges oldcats into a single new category, newcat. Any values in A from oldcats become newcat in B.

Examples

collapse all

Create a categorical array containing various colors.

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

     red 
     blue 
     pink 
     red 
     blue 
     red 

A is a 6-by-1 categorical array.

Display the categories of A.

categories(A)
ans =

  3×1 cell array

    'blue'
    'pink'
    'red'

The three categories are in alphabetical order.

Merge the categories red and pink into the category red. Specify red first in oldcats to use it as the merged category.

oldcats = {'red','pink'};
B = mergecats(A,oldcats)
B = 

     red 
     blue 
     red 
     red 
     blue 
     red 

mergecats replaces the value pink from A(3) with red.

Display the categories of B.

categories(B)
ans =

  2×1 cell array

    'blue'
    'red'

B has two categories instead of three.

Create a categorical array containing various items.

A = categorical({'shirt' 'pants'; 'shoes' 'shirt'; 'dress' 'belt'})
A = 

     shirt      pants 
     shoes      shirt 
     dress      belt  

Display the categories of A.

categories(A)
ans =

  5×1 cell array

    'belt'
    'dress'
    'pants'
    'shirt'
    'shoes'

The five categories are in alphabetical order.

Merge the categories belt and shoes into a new category called other.

B = mergecats(A,{'belt' 'shoes'},'other')
B = 

     shirt      pants 
     other      shirt 
     dress      other 

The value other replaces all instances of belt and shoes.

Display the categories of B.

categories(B)
ans =

  4×1 cell array

    'other'
    'dress'
    'pants'
    'shirt'

B has four categories and the order is no longer alphabetical. other appears in place of belt.

Create an ordinal categorical array.

A = categorical([1 2 3 2 1],1:3,{'poor','fair','good'},'Ordinal',true)
A = 

     poor      fair      good      fair      poor 

Display the categories of A.

categories(A)
ans =

  3×1 cell array

    'poor'
    'fair'
    'good'

Since A is ordinal, the categories have the mathematical ordering poor < fair < good.

Consider all fair or poor values to be bad. Since A is ordinal, the categories to merge must be consecutive.

B = mergecats(A,{'fair' 'poor'},'bad')
B = 

     bad      bad      good      bad      bad 

The value bad replaces all instances of fair and poor.

Display the categories of B.

categories(B)
ans =

  2×1 cell array

    'bad'
    'good'

B has two categories with the mathematical ordering: bad < good.

Input Arguments

collapse all

Categorical array, specified as a vector, matrix, or multidimensional array.

Categories to merge, specified as a cell array of character vectors. If A is ordinal, the categories to merge must be consecutive.

New category, specified as a character vector.

More About

collapse all

Tall Array Support

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

Introduced in R2013b

Was this topic helpful?