主要内容

addcats

将类别添加到分类数组

说明

B = addcats(A,newcats) 将类别添加到分类数组中。默认情况下,addcats 将新类别添加到类别集的末尾,如 categories(A) 的输出所示。它不添加新元素。输出分类数组 B 不包含属于任何新类别的元素,除非您将这些元素分配给 B

如果 A 是一个有序分类数组,那么必须指定 BeforeAfter 名称-值参量。

示例

B = addcats(A,newcats,Before=catname) 在指定类别之前添加类别。

示例

B = addcats(A,newcats,After=catname) 在指定类别之后添加类别。

示例

全部折叠

创建一个分类数组。

A = categorical(["red" "blue" "red" "blue" "red" "blue"])
A = 1×6 categorical
     red      blue      red      blue      red      blue 

显示数组的类别。

categories(A)
ans = 2×1 cell
    {'blue'}
    {'red' }

将类别 greenblack 添加到类别集的末尾。新数组包含相同的元素,但有更多类别。

B = addcats(A,["green" "black"])
B = 1×6 categorical
     red      blue      red      blue      red      blue 

categories(B)
ans = 4×1 cell
    {'blue' }
    {'red'  }
    {'green'}
    {'black'}

创建一个有序分类数组。

A = categorical(["medium" "large"; "small" "xlarge"; "large" "medium"], ...
                ["small" "medium" "large" "xlarge"], ...
                Ordinal=true)
A = 3×2 categorical
     medium      large  
     small       xlarge 
     large       medium 

显示类别。由于 A 是有序数组,因此,其类别采用数学排序 small < medium < large < xlarge

categories(A)
ans = 4×1 cell
    {'small' }
    {'medium'}
    {'large' }
    {'xlarge'}

将类别 xsmall 添加到 small 之前。然后显示新数组的类别。其类别的数学顺序为 xsmall < small < medium < large < xlarge

B = addcats(A,"xsmall",Before="small");
categories(B)
ans = 5×1 cell
    {'xsmall'}
    {'small' }
    {'medium'}
    {'large' }
    {'xlarge'}

输入参数

全部折叠

输入数组,指定为分类数组。

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

类别的名称,指定为字符串标量或字符向量。

扩展功能

全部展开

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

版本历史记录

在 R2013b 中推出