mergecats
合并 categorical
数组中的类别
说明
示例
将两个类别合并成一个
创建包含各种颜色的分类数组。
A = categorical({'red';'blue';'pink';'red';'blue';'red'})
A = 6x1 categorical
red
blue
pink
red
blue
red
A
是 6×1 分类数组。
显示 A
的类别。
categories(A)
ans = 3x1 cell
{'blue'}
{'pink'}
{'red' }
这三种类别按字母顺序排序。
将类别 red
和 pink
合并成类别 red
。先指定 oldcats
中的 red
,以将其用作合并类别。
oldcats = {'red','pink'}; B = mergecats(A,oldcats)
B = 6x1 categorical
red
blue
red
red
blue
red
mergecats
将 A(3)
中的值 pink
替换成 red
。
显示 B
的类别。
categories(B)
ans = 2x1 cell
{'blue'}
{'red' }
B
包含两个而非三个类别。
合并按字母顺序列出的类别
创建包含各种物品的分类数组。
A = categorical({'shirt' 'pants'; 'shoes' 'shirt'; 'dress' 'belt'})
A = 3x2 categorical
shirt pants
shoes shirt
dress belt
显示 A
的类别。
categories(A)
ans = 5x1 cell
{'belt' }
{'dress'}
{'pants'}
{'shirt'}
{'shoes'}
这五种类别按字母顺序排序。
将类别 belt
和 shoes
合并成名为 other
的新类别。
B = mergecats(A,{'belt' 'shoes'},'other')
B = 3x2 categorical
shirt pants
other shirt
dress other
值 other
将替换 belt
和 shoes
的所有实例。
显示 B
的类别。
categories(B)
ans = 4x1 cell
{'other'}
{'dress'}
{'pants'}
{'shirt'}
B
包含四个类别,且不再按字母顺序排序。原来的 belt
改示为 other
。
合并有序分类数组的类别
创建一个有序分类数组。
A = categorical([1 2 3 2 1],1:3,{'poor','fair','good'},'Ordinal',true)
A = 1x5 categorical
poor fair good fair poor
显示 A
的类别。
categories(A)
ans = 3x1 cell
{'poor'}
{'fair'}
{'good'}
由于 A
是有序数组,因此,其类别采用数学排序 poor < fair < good
。
考虑将所有 fair
或 poor
值变成 bad
。由于 A
是有序数组,因此,要合并的类别必须具有连续性。
B = mergecats(A,{'fair' 'poor'},'bad')
B = 1x5 categorical
bad bad good bad bad
值 bad
将替换 fair
和 poor
的所有实例。
显示 B
的类别。
categories(B)
ans = 2x1 cell
{'bad' }
{'good'}
B
包含两个采用数学排序法排序的类别:bad < good
。
使用模式指定类别
创建一个 categorical
数组。此数组有许多不同类别可以表示“是”和“否”。
C = categorical(["Y" "Yes" "Yeah" "N" "No" "Nope"])
C = 1x6 categorical
Y Yes Yeah N No Nope
categories(C)
ans = 6x1 cell
{'N' }
{'No' }
{'Nope'}
{'Y' }
{'Yeah'}
{'Yes' }
您可以使用pattern
匹配多个类别名称。例如,要指定以 Y
开头的类别名称,可以使用通配符模式。要创建通配符模式,请使用 wildcardPattern
函数。
将名称以 Y
开头的所有类别合并为一个名为 yes
的类别。然后将名称以 N
开头的所有类别合并为一个名为 no
的类别。因此,具有相同含义的值都属于同一类别。现在,C
只有两个类别。
C = mergecats(C,"Y" + wildcardPattern,"yes"); C = mergecats(C,"N" + wildcardPattern,"no")
C = 1x6 categorical
yes yes yes no no no
categories(C)
ans = 2x1 cell
{'no' }
{'yes'}
输入参数
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2013b 中推出
另请参阅
categories
| addcats
| removecats
| iscategory
| renamecats
| reordercats
| setcats
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)