Main Content

setcats

Set categories in categorical array

Description

B = setcats(A,newcats) sets categories in a categorical array. The output array B has the categories specified by newcats and elements that correspond to the elements of the input array 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.

example

Examples

collapse all

Create a categorical array.

A = categorical(["blue" "black" "red" "red" "blue" "black" "black" "red" "blue"])
A = 1x9 categorical
     blue      black      red      red      blue      black      black      red      blue 

Set new categories. Elements that were blue in A are undefined in B.

B = setcats(A,["red" "black"])
B = 1x9 categorical
     <undefined>      black      red      red      <undefined>      black      black      red      <undefined> 

Create a categorical array.

A = categorical(["blue" "black" "red" "red" "blue" "black" "black" "red" "blue"])
A = 1x9 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 = 1x9 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) = "pink";
B
B = 1x9 categorical
     pink      <undefined>      red      red      blue      <undefined>      <undefined>      red      blue 

Input Arguments

collapse all

Input array, specified as a categorical array.

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

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