How Do I Determine which Group Variables Correspond to the Results of SplitApply
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
>> load patients
>> 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.
0 Commenti
Risposta accettata
Peter O
il 30 Ott 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!