categorical

Methods

Method Reference: 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 of A specified in cats, which may be a categorical array, a string array, or a cell array of character vectors, as long as it specifies unique existing categories in A.

N = histcounts (…, 'Normalization', normtype) specifies how to normalize the histogram values returned in N with any of the following options specified in 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.
  • '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

Example: 1

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'}