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개)

David Hill
David Hill 2020년 9월 2일

0 개 추천

m=nnz(t.1stcolumn=='true')/sum(t.lastcolumn);
Ameer Hamza
Ameer Hamza 2020년 9월 2일
편집: Ameer Hamza 2020년 9월 2일

0 개 추천

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

질문:

2020년 9월 2일

편집:

2020년 9월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by