Function Reference: groupcounts

datatypes: B = groupcounts (A)
datatypes: B = groupcounts (A, groupbins)
datatypes: [B, BG, BP] = groupcounts (…)
datatypes: […] = groupcounts (…, Name, Value)

Count the number of elements in each group of an array.

B = groupcounts (A) groups the rows of A by their values and returns B, a column vector with the number of elements in each group. A is a grouping vector, a matrix whose columns are grouping variables, or a cell array of grouping vectors. Groups are the sorted unique combinations of grouping values; rows holding a missing value in a grouping variable form their own groups, sorted after the non-missing groups.

[B, BG, BP] = groupcounts (…) also returns BG, the grouping values that identify each group, and BP, a column vector giving each group’s count as a percentage of the total. When A is a single grouping vector, BG holds its representative value for each group; when several grouping variables are given, BG is a cell array with one element per grouping variable.

The optional groupbins argument bins the grouping variables before grouping (a vector of bin edges or a positive integer number of bins, applied to a numeric, datetime, or duration grouping variable, or a cell array with one scheme per grouping variable); each binned variable becomes a categorical of bin interval labels.

The behaviour can be modified with the 'IncludeMissingGroups' (default true), 'IncludeEmptyGroups' (default false), and 'IncludedEdge' (default 'left', the inclusive bin edge) Name/Value pairs, as for the table method. When 'IncludeEmptyGroups' is true, the unused categories of a categorical or binned grouping variable contribute empty groups.

To count the rows in each group of a table, call groupcounts (T, groupvars, …), which dispatches to the table method and returns the result as a table.

See also: groupsummary, findgroups, splitapply, table

Source Code: groupcounts