How to use the 'splitapply' function for columns of a table?

15 vues (au cours des 30 derniers jours)
MathWorks Support Team
MathWorks Support Team le 9 Août 2019
Commenté : Walter Roberson le 22 Oct 2021
Can the 'splitapply' function work on multiple columns on a table without using 'for' loop?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 9 Août 2019
You can achieve this after converting the table into a matrix and then using the 'splitapply' function.
Please find the code snippet for a similar example below:
Age = [38; 43; 38; 40; 49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
Gender = {'F';'F';'M';'F';'M'};
T = table(Age,Height,Weight,Gender);
[G,gender] = findgroups(T.Gender);
Tarray = table2array(T(:,1:end-1));
out_mean = splitapply(@mean,Tarray,G);
Please find additional information on the 'splitapply' function in the documentation link below:
  2 commentaires
Robert
Robert le 22 Oct 2021
This errors when any group only has one member. For example, if the genders are
Gender = {'F';'F';'M';'F';'F'};
I get an error when calling splitapply afterwards.
Is there any other solution?
Walter Roberson
Walter Roberson le 22 Oct 2021
out_mean = splitapply(@(x) mean(x, 1),Tarray,G);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Analysis dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by