Function Reference: groupfilter

datatypes: B = groupfilter (A, groupvars, method)
datatypes: B = groupfilter (A, groupvars, groupbins, method)
datatypes: [B, BG] = groupfilter (…)

Filter the rows of an array by a per-group condition.

B = groupfilter (A, groupvars, method) groups the rows of the array A by the grouping variables groupvars, applies the filter function method to each group, and returns B, the rows of A that satisfy the condition, in their original order. groupvars is a grouping vector with one element per row of A, or a cell array of such vectors. Rows holding a missing value in a grouping variable form their own groups, to which method is applied like any other group.

method is a function handle applied to each group’s slice of every column of A. It must return either a logical scalar, which keeps or drops the whole group, or a logical vector with one element per row of the group, which keeps or drops the individual rows. A row is kept only when the condition holds for it across all columns of A.

[B, BG] = groupfilter (…) also returns BG, the grouping values of the kept rows. When groupvars is a single grouping vector, BG is a column vector with one element per kept row; 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, or a cell array with one scheme per grouping variable); see groupsummary for details. The 'IncludedEdge' Name-Value pair ('left' by default, or 'right') selects which bin edge is inclusive.

To filter the rows of a table, call groupfilter (T, groupvars, …), which dispatches to the table method and returns the result as a table.

See also: groupsummary, groupcounts, findgroups, splitapply, table

Source Code: groupfilter