countcats
Count occurrences of categorical array elements by category
Description
B = countcats(
returns
the number of elements in each category of the categorical array, A
)A
.
If
A
is a vector, thencountcats
returns the number of elements in each category.If
A
is a matrix, thencountcats
treats the columns ofA
as vectors and returns the category counts for each column ofA
.If
A
is a multidimensional array, thencountcats
acts along the first array dimension whose size does not equal1
.
To display a summary of a categorical array that shows both category names and the
number of elements in each category, use the summary
function.
Examples
Category Counts of Categorical Vector
Create a 1-by-5 categorical vector.
A = categorical({'plane' 'car' 'train' 'car' 'plane'})
A = 1x5 categorical
plane car train car plane
A
has three categories. To list the categories and their order, use the categories
function. Every element of A
belongs to one of the categories listed by the call to categories
.
categories(A)
ans = 3x1 cell
{'car' }
{'plane'}
{'train'}
Find the number of elements in each category of A
.
B = countcats(A)
B = 1×3
2 2 1
The first element in B
corresponds to the first category of A
, which is car
. The second element in B
corresponds to the second category of A
, which is plane
. The third element of B
corresponds to the third category of A
, which is train
.
Since A
is a row vector, countcats
returns a row vector.
Category Counts of Each Column in Array
Create a 3-by-2 categorical array, A
, from a numeric array.
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
has three categories, red
, green
, and blue
.
Find the category counts of each column in A
.
B = countcats(A)
B = 3×2
1 2
1 0
1 1
The first row of B
corresponds to the first category of A
. The value, red
, occurs once in the first column of A
and twice in the second column.
The second row of B
corresponds to the second category of A
. The value, green
, occurs once in the first column of A
, and it does not occur in the second column.
The third row of B
corresponds to the third category of A
. The value, blue
, occurs once in the first column of A
and once in the second column.
Category Counts of Each Row in Array
Create a 3-by-2 categorical array, A
, from a numeric array.
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
has three categories, red
, green
, and blue
.
Find the category counts of A
along the second dimension.
B = countcats(A,2)
B = 3×3
1 0 1
1 1 0
1 0 1
The first column of B
corresponds to the first category of A
. The value, red
, occurs once in the first row of A
, once in the second row, and once in the third row.
The second column of B
corresponds to the second category of A
. The value, green
, occurs in only one element. It occurs in the second row of A
.
The third column of B
corresponds to the third category of A
. The value, blue
, occurs once in the first row of A
and once in the third row.
Category Counts of Array Containing Undefined Elements
Create a 6-by-1 categorical array, A
, from a numeric array.
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
Remove the blue
category.
A = removecats(A,'blue')
A = 6x1 categorical
red
<undefined>
green
red
<undefined>
red
A
has two categories, red
and green
. Elements of A
that were from the blue
category are now undefined.
Find the number of elements in each category of A
.
B = countcats(A)
B = 2×1
3
1
The first element in B
corresponds to the first category of A
. The value, red
, occurs three times in A
.
The second element in B
corresponds to the second category of A
. The value, green
, occurs once in A
.
countcats
does not return any information on undefined elements.
Use the summary
function to view the number of undefined elements in addition to the number of elements in each category of A
.
summary(A)
red 3 green 1 <undefined> 2
Input Arguments
A
— Categorical array
vector | matrix | multidimensional array
Categorical array, specified as a vector, matrix, or multidimensional array.
dim
— Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.
Consider a two-dimensional categorical array, A
.
If dim = 1
, then countcats(A,1)
returns
the category counts for each column of A
.
If dim = 2
, then countcats(A,2)
returns the category
counts for each row of A
.
If dim
is greater than ndims(A)
,
then countcats(A)
returns an array the same size
as A
for each category. countcats
returns 1
for
elements in the corresponding category and 0
otherwise.
Tips
To find the number of undefined elements in a categorical array,
A
, you must usesummary
orisundefined
.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2013b
See Also
iscategory
| ismember
| summary
| isundefined
| categories
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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.
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)