Main Content

setcats

Set categories in categorical array

Description

example

B = setcats(A,newcats) sets categories in output categorical array B using categories defined by newcats and elements defined by A.

  • If an element of A is in a category listed in newcats, then the corresponding element of B has the same value as in A.

  • If A has a category not listed in newcats, then B does not have that category. The corresponding elements in B are undefined.

  • If newcats lists a category that is not a category of A, then B has no elements equal to that category.

Examples

collapse all

Create a categorical array containing various colors.

A = categorical({'blue','black','red';'red','blue','black';'black','red','blue'})
A = 3x3 categorical
     blue       black      red   
     red        blue       black 
     black      red        blue  

Set new categories.

B = setcats(A,{'red','black'})
B = 3x3 categorical
     <undefined>      black            red         
     red              <undefined>      black       
     black            red              <undefined> 

Elements that were blue in A are undefined in B.

Create a categorical array containing various colors.

A = categorical({'blue','black','red';'red','blue','black';'black','red','blue'})
A = 3x3 categorical
     blue       black      red   
     red        blue       black 
     black      red        blue  

Set new categories. Include a category that is not a category of A.

B = setcats(A,{'red','pink','blue'})
B = 3x3 categorical
     blue             <undefined>      red         
     red              blue             <undefined> 
     <undefined>      red              blue        

No element of B is pink, because pink is not a category of A. Assign an element of B to be pink.

B(1,2) = 'pink';
B
B = 3x3 categorical
     blue             pink      red         
     red              blue      <undefined> 
     <undefined>      red       blue        

Input Arguments

collapse all

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

New categories, specified as a character vector, a cell array of character vectors, or a string array.

Tips

  • To change category names in a categorical array, use renamecats.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2014b