MATLAB Answers

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

53 views (last 30 days)
Can the 'splitapply' function work on multiple columns on a table without using 'for' loop?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Aug 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:


Sign in to comment.

More Answers (0)