# How Do I Determine which Group Variables Correspond to the Results of SplitApply

6 views (last 30 days)
Paul on 30 Oct 2020
Commented: Paul on 30 Oct 2020
Suppose I use splitapply as follows, taken directly from its doc page:
>> whos Gender Height
Name Size Bytes Class Attributes
Gender 100x1 12212 cell
Height 100x1 800 double
>> G = findgroups(Gender);
>> splitapply(@mean,Height,G)
ans =
6.5151e+01
6.9234e+01
At this point, the doc page says: "The first row of the output argument is the mean height of the female patients, and the second row is the mean height of the male patients." But it doesn't say how to determine that is, in fact, the case. In other words, how can I tell that G ordered the group variables as female followed by male, rather than the opposite. The doc page for findgroups basically suggests to use a visual inspection of G, but that's not practical for large arrays with lots of groups. Also, I'd like to be able to get the group variables out from Gender in the same order that they correspond to the output from splitapply, e.g., for plotting or axis labeling. So far, I've come up with this
>> Gvar = splitapply(@(x)(x(1)),Gender,G)
Gvar =
2×1 cell array
{'Female'}
{'Male' }
Is there not a Matlab-provided function to do this? If so, I couldn't find it and was surprised as it seems to me that this would be a pretty common need.

Peter O on 30 Oct 2020
Consider using the second argument output from findgroups?
[G,ID] = findgroups(A) also returns the unique values for each group in ID. For example, if A is {'b','a','a','b'}, then findgroups returns G as [2 1 1 2] and ID as {'a','b'}. The arguments A and ID are the same data type, but need not be the same size.
[G, ID] = findgroups(Gender)
ID =
2×1 cell array
{'Female'}
{'Male' }
The group IDs are sequential, so ID{1} will always match the entries for G==1, ID{2} matches G==2, etc. The grouping is usually sorted.
Paul on 30 Oct 2020
Ouch. I just slapped my head. No idea how I missed that. Thanks!

R2019a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!