countcats
按类别统计分类数组元素的出现次数
说明
示例
创建一个分类数组。
A = categorical(["red" "blue" "green" "blue" "red"],["red" "green" "blue"])
A = 1×5 categorical
red blue green blue red
它包含三个类别:red、green 和 blue。
categories(A)
ans = 3×1 cell
{'red' }
{'green'}
{'blue' }
计算 A 的每个类别中的元素数量。
B中的第一个元素对应于A的第一个类别,即red。B中的第二个元素对应于A的第二个类别,即green。B中的第三个元素对应于A的第三个类别,即blue。
B = countcats(A)
B = 1×3
2 1 2
使用 summary 函数可以看到相同的结果。
summary(A)
A: 1×5 categorical
red green blue <undefined>
2 1 2 0
创建一个 3×2 分类数组。
valueset = 1:3; catnames = ["red" "green" "blue"]; A = categorical([1 3; 2 1; 3 1],valueset,catnames)
A = 3×2 categorical
red blue
green red
blue red
它包含三个类别:red、green 和 blue。
categories(A)
ans = 3×1 cell
{'red' }
{'green'}
{'blue' }
计算 A 中每列的类别计数。计数的顺序与 categories 函数列出的类别顺序相同。
值
red在A的第一列中出现一次,在第二列中出现两次。值
green在A的第一列中出现一次,在第二列中不出现。值
blue在A的第一列中出现一次,在第二列中出现一次。
B = countcats(A)
B = 3×2
1 2
1 0
1 1
使用 summary 函数可以看到相同的结果。
summary(A)
A: 3×2 categorical
red 1 2
green 1 0
blue 1 1
<undefined> 0 0
创建一个 3×2 分类数组。
valueset = 1:3; catnames = ["red" "green" "blue"]; A = categorical([1 3; 2 1; 3 1],valueset,catnames)
A = 3×2 categorical
red blue
green red
blue red
它包含三个类别:red、green 和 blue。
categories(A)
ans = 3×1 cell
{'red' }
{'green'}
{'blue' }
计算 A 沿第二个维度的类别计数。计数的顺序与 categories 函数列出的类别顺序相同。
值
red在A的第一行以及第二行、第三行中出现一次。值
green仅出现在一个元素中。该元素位于A的第二行中。值
blue在A的第一行中出现一次,在第三行中出现一次。
B = countcats(A,2)
B = 3×3
1 0 1
1 1 0
1 0 1
使用 summary 函数可以看到相同的结果。
summary(A,2)
A: 3×2 categorical
red green blue <undefined>
1 0 1 0
1 1 0 0
1 0 1 0
countcats 函数不会返回分类数组中未定义元素的数量。
创建一个 6×1 分类数组。
valueset = 1:3; catnames = ["red" "green" "blue"]; A = categorical([1;3;2;1;3;1],valueset,catnames)
A = 6×1 categorical
red
blue
green
red
blue
red
删除 blue 类别。A 有两个类别,即可 red 和 green。属于 blue 类别的 A 的元素现在未定义。
A = removecats(A,"blue")A = 6×1 categorical
red
<undefined>
green
red
<undefined>
red
计算 A 的每个类别中的元素数量。
值
red在A中出现三次。值
green在A中出现一次。countcats不返回未定义元素的数量。
B = countcats(A)
B = 2×1
3
1
要查看未定义元素的数量以及 A 的每个类别中的元素数,请使用 summary 函数。
summary(A)
A: 6×1 categorical
red 3
green 1
<undefined> 2
输入参数
输入数组,指定为分类数组。
沿其运算的维度,指定为正整数标量。如果不指定维度,则默认值是大小不等于 1 的第一个数组维度。
以一个二维分类数组 A 为例。
如果 dim = 1,则 countcats(A,1) 返回 A 的每列的类别计数。

如果 dim = 2,则 countcats(A,2) 返回 A 的每行的类别计数。

如果 dim 大于 ndims(A),则 countcats(A) 为每个类别返回大小与 A 相同的数组。countcats 为对应类别中的元素返回 1,否则返回 0。
提示
要计算分类数组中未定义的元素数,必须使用
summary或isundefined。
扩展功能
countcats 函数完全支持 tall 数组。有关详细信息,请参阅 tall 数组。
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
此函数完全支持分布式数组。有关详细信息,请参阅使用分布式数组运行 MATLAB 函数 (Parallel Computing Toolbox)。
版本历史记录
在 R2013b 中推出
另请参阅
iscategory | ismember | summary | isundefined | categories
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)