splitapply: further subdividing
Show older comments

So i have a table above and I want to use splitapply to find the mean. So the mean would come from this:
#number of trues
---------------------------------
sum of last column (ignore the middle column)
How should I go about this? I feel like it is an easy solution but I am overthinking due to the logicals. Thank you so much!
Answers (2)
David Hill
on 2 Sep 2020
m=nnz(t.1stcolumn=='true')/sum(t.lastcolumn);
Ameer Hamza
on 2 Sep 2020
Edited: Ameer Hamza
on 2 Sep 2020
Try something like this
t = table([true;false;false;true;false], [1;2;3;4;5], [5;4;3;2;1]); % for example
disp(t);
mean_vals = splitapply(@mean, t.Var3, t.Var1+1);
fprintf('Mean of false values: %f\n', mean_vals(1));
fprintf('Mean of True values: %f\n', mean_vals(2));
Output
Var1 Var2 Var3
_____ ____ ____
true 1 5
false 2 4
false 3 3
true 4 2
false 5 1
Mean of false values: 2.666667
Mean of True values: 3.500000
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!