Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

setcats

Set categories in categorical array

Syntax

B = setcats(A,newcats)

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 array
     blue       black      red   
     red        blue       black 
     black      red        blue  

Set new categories.

B = setcats(A,{'red','black'})
B = 3x3 categorical array
     <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 array
     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 array
     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 array
     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 or a cell array of character vectors.

Tips

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

Extended Capabilities

Introduced in R2014b

Was this topic helpful?