Main Content

setcats

设置分类数组中的类别

说明

示例

B = setcats(A,newcats) 使用 newcats 定义的类别和 A 定义的元素设置输出分类数组 B 中的类别。

  • 如果 A 的元素处于 newcats 中列出的类别中,则 B 的对应元素具有与 A 中的元素相同的值。

  • 如果 A 具有 newcats 中未列出的类别,则 B 不包含该类别。B 中的对应元素未定义。

  • 如果 newcats 列出了不是 A 的类别的类别,则 B 不包含与该类别相等的元素。

示例

全部折叠

创建包含各种颜色的分类数组。

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

设置新类别。

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

A 中颜色为 blue 的元素未在 B 中定义。

创建包含各种颜色的分类数组。

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

设置新类别。包括不是 A 中类别的类别。

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

B 的元素都不为 pink,因为 pink 不是 A 的类别。将 B 的一个元素分配为 pink

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

输入参数

全部折叠

分类数组,指定为向量、矩阵或多维数组。

新类别,指定为字符向量、字符向量元胞数组或字符串数组。

提示

  • 要更改分类数组中的类别名称,请使用 renamecats

扩展功能

C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。

版本历史记录

在 R2014b 中推出