splitapply: further subdividing

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!

回答(2 个)

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

类别

帮助中心File Exchange 中查找有关 Tables 的更多信息

产品

版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by