countcats
按类别统计分类数组元素的出现次数
说明
示例
分类向量的类别计数
创建一个 1×5 分类向量。
A = categorical({'plane' 'car' 'train' 'car' 'plane'})
A = 1x5 categorical
plane car train car plane
A
包含三个类别。要列出类别及其顺序,请使用 categories
函数。A
的每个元素都属于通过调用 categories
所列出的类别之一。
categories(A)
ans = 3x1 cell
{'car' }
{'plane'}
{'train'}
计算 A
的每个类别中的元素数量。
B = countcats(A)
B = 1×3
2 2 1
B
中的第一个元素对应于 A
的第一个类别,即 car
。B
中的第二个元素对应于 A
的第二个类别,即 plane
。B
中的第三个元素对应于 A
的第三个类别,即 train
。
因为 A
是行向量,所以 countcats
返回行向量。
数组中每列的类别计数
从一个数值数组创建一个 3×2 分类数组 A
。
valueset = 1:3; catnames = {'red' 'green' 'blue'}; A = categorical([1 3; 2 1; 3 1],valueset,catnames)
A = 3x2 categorical
red blue
green red
blue red
A
包含三个类别:red
、green
和 blue
。
计算 A
中每列的类别计数。
B = countcats(A)
B = 3×2
1 2
1 0
1 1
B
的第一行对应于 A
的第一个类别。值 red
在 A
的第一列中出现一次,在第二列中出现两次。
B
的第二行对应于 A
的第二个类别。值 green
在 A
的第一列中出现一次,在第二列中不出现。
B
的第三行对应于 A
的第三个类别。值 blue
在 A
的第一列中出现一次,在第二列中出现一次。
数组中每行的类别计数
从一个数值数组创建一个 3×2 分类数组 A
。
valueset = 1:3; catnames = {'red' 'green' 'blue'}; A = categorical([1 3; 2 1; 3 1],valueset,catnames)
A = 3x2 categorical
red blue
green red
blue red
A
包含三个类别:red
、green
和 blue
。
计算 A
沿第二个维度的类别计数。
B = countcats(A,2)
B = 3×3
1 0 1
1 1 0
1 0 1
B
的第一列对应于 A
的第一个类别。值 red
在 A
的第一行以及第二行、第三行中出现一次。
B
的第二列对应于 A
的第二个类别。值 green
仅出现在一个元素中。该元素位于 A
的第二行中。
B
的第三列对应于 A
的第三个类别。值 blue
在 A
的第一行中出现一次,在第三行中出现一次。
包含未定义元素的数组的类别计数
从一个数值数组创建一个 6×1 分类数组 A
。
valueset = 1:3; catnames = {'red' 'green' 'blue'}; A = categorical([1;3;2;1;3;1],valueset,catnames)
A = 6x1 categorical
red
blue
green
red
blue
red
删除类别 blue
。
A = removecats(A,'blue')
A = 6x1 categorical
red
<undefined>
green
red
<undefined>
red
A
包含两个类别:red
和 green
。blue
中来自 A
类别的元素现在未定义。
计算 A
的每个类别中的元素数量。
B = countcats(A)
B = 2×1
3
1
B
中的第一个元素对应于 A
的第一个类别。值 red
在 A
中出现三次。
B
中的第二个元素对应于 A
的第二个类别。值 green
在 A
中出现一次。
countcats
不返回有关未定义元素的任何信息。
使用 summary
函数查看未定义元素数以及 A
的每个类别中的元素数。
summary(A)
red 3 green 1 <undefined> 2
输入参数
A
— 分类数组
向量 | 矩阵 | 多维数组
分类数组,指定为向量、矩阵或多维数组。
dim
— 沿其运算的维度
正整数标量
沿其运算的维度,指定为正整数标量。如果不指定维度,则默认为第一个大于 1 的数组维度。
以一个二维分类数组 A
为例。
如果 dim = 1
,则 countcats(A,1)
返回 A
的每列的类别计数。
如果 dim = 2
,则 countcats(A,2)
返回 A
的每行的类别计数。
如果 dim
大于 ndims(A)
,则 countcats(A)
为每个类别返回大小与 A
相同的数组。countcats
为对应类别中的元素返回 1
,否则返回 0
。
提示
要计算分类数组
A
中未定义的元素数,必须使用summary
或isundefined
。
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
此函数完全支持 tall 数组。有关详细信息,请参阅 tall 数组。
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅Run MATLAB Functions in Thread-Based Environment。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2013b 中推出
另请参阅
iscategory
| ismember
| summary
| isundefined
| categories
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)