table.findgroups
table: G = findgroups (T)
table: [G, TID] = findgroups (T)
Find groups defined by the variables of a table.
G = findgroups (T) returns G, a column vector of
positive integer group numbers, with one element for each row of the
table T. Each variable of T acts as a grouping variable, and
the groups are the unique combinations of values across those variables,
sorted in ascending order. If N groups are found, every integer
between 1 and N labels a group. Rows holding a missing value
(NaN, NaT, <missing>, '', or
<undefined>) in any grouping variable are labelled NaN in
G.
[G, TID] = findgroups (T) also returns
TID, a table whose rows are the sorted unique combinations
identifying each group, with the same variables as T.
Source Code: table
findgroups assigns each row an integer group number based on its grouping variable(s) — the setup step for a splitapply computation. A second output returns a table naming each group.
Species = {'setosa'; 'virginica'; 'setosa'; 'virginica'; 'setosa'};
Petal = [1.4; 5.1; 1.5; 5.9; 1.3];
T = table (Species, Petal)
T =
5x2 table
Species Petal
_____________ _____
{'setosa' } 1.4
{'virginica'} 5.1
{'setosa' } 1.5
{'virginica'} 5.9
{'setosa' } 1.3
[G, groupNames] = findgroups (T(:, 'Species')); G'
ans = 1 2 1 2 1
groupNames
groupNames =
2x1 table
Species
_____________
{'setosa' }
{'virginica'}
The group numbers index into groupNames, and feed straight into splitapply to aggregate Petal per species.
splitapply (@mean, T(:, 'Petal'), G)
ans = 1.4000 5.5000