Documentation

setcats

Set categories in categorical array

Syntax

B = setcats(A,newcats)

Description

example

B = setcats(A,newcats)sets categories in output categorical arrayBusing categories defined bynewcatsand elements defined byA.

  • If an element ofAis in a category listed innewcats, then the corresponding element ofBhas the same value as inA.

  • IfAhas a category not listed innewcats, thenBdoes not have that category. The corresponding elements inBare undefined.

  • Ifnewcatslists a category that is not a category ofA, thenBhas no elements equal to that category.

Examples

collapse all

Create a categorical array containing various colors.

A = categorical({'blue','black',“红色”;“红色”,'blue','black';'black',“红色”,'blue'})
A =3x3 categorical arrayblue black red red blue black black red blue

Set new categories.

B = setcats(A,{“红色”,'black'})
B =3x3 categorical array black red red  black black red 

Elements that wereblueinAare undefined inB.

Create a categorical array containing various colors.

A = categorical({'blue','black',“红色”;“红色”,'blue','black';'black',“红色”,'blue'})
A =3x3 categorical arrayblue black red red blue black black red blue

Set new categories. Include a category that is not a category ofA.

B = setcats(A,{“红色”,'pink','blue'})
B =3x3 categorical arrayblue  red red blue   red blue

No element ofBispink, becausepinkis not a category ofA. Assign an element ofBto bepink.

B(1,2) ='pink'; B
B =3x3 categorical arrayblue pink red red blue   red blue

Input Arguments

collapse all

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

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

Tips

  • To change category names in a categorical array, userenamecats.

Extended Capabilities

Introduced in R2014b

Was this topic helpful?