categorical.histcounts
categorical: N = histcounts (A)
categorical: N = histcounts (A, cats)
categorical: N = histcounts (…, 'Normalization', normtype)
categorical: [N, cats] = histcounts (…)
Histogram bin counts of a categorical array.
N = histcounts (A) returns a numeric vector N
with the number of elements of each category in A. A can be
a categorical array of any dimensions, but it is converted internally to
a single column vector.
N = histcounts (A, cats) returns the number of
elements only for the categories named in cats, in the order they
are named there. cats may be a categorical array, a string array,
or a cell array of character vectors, and the names must be unique. A
name that is not a category of A is not an error: it counts zero.
Note that <undefined> elements are counted under no name at all,
so they never appear in N.
N = histcounts (…, specifies how to normalize the histogram values returned
in N with any of the following options specified in normtype:
'Normalization',
normtype)
'count', which is the default, returns the number of
elements in each category.
'countdensity' is the same as 'count', since the
bin width in categorical arrays is always equal to 1.
'probability' returns the number of elements in each
category relative to the total number of elements in A.
'percentage' returns the percentage of the elements of
A that fall in each category.
'pdf' is the same as 'probability', since the bin
width in categorical arrays is always equal to 1.
'cumcount' returns the cumulative number of elements in
each category and all previous categories.
'cdf' returns the cumulative number of elements in
each category and all previous categories relative to the total number of
elements in A.
[N, cats] = histcounts (…) also returns the
corresponding categories of A for each count in N.
cats is a cell array of character vectors with the same size as
N.
Source Code: categorical
histcounts bins a categorical array by category and returns the count in each — the histogram companion of countcats, with a second output naming the bins.
C = categorical ({'M'; 'S'; 'L'; 'M'; 'S'; 'M'}, {'S', 'M', 'L'})
C =
6x1 categorical array
M
S
L
M
S
M
[N, cats] = histcounts (C); N
N = 2 3 1
cats
cats =
1x3 cell array
{'S'} {'M'} {'L'}