Table Mean and Standard Deviation
3 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have a table and I would like to find out the mean and standard deviation of a column under the condition of two other columns.
z1 z2 z3 z4
___ __ __ __
-1 0 0 1
0 1 2 0
1 3 2 1
2 5 2 1
3 2 0 1
4 4 0 1
5 2 2 1
6 1 4 0
7 0 4 0
z2 has a range of 1:5 and z3 is devided into 0,2,4
So I would like to calculate the mean of z4 under the condition of z2 = 1, z3 = 0 and then z2 = 2, z3 = 0, z2= 1, z3 =2 etc.
I think I need to use logical indexing, but MATLAB gave me an error because it is a table. Can anyone help? Thank you so much!
1 件のコメント
WalterWhite
2021 年 1 月 13 日
you can also import the table as coloumn vectors and perform the logical indexing
回答 (1 件)
Ive J
2021 年 1 月 13 日
try this
head(x)
z1 z2 z3 z4
__ __ __ _______
7 2 4 0.88646
4 1 0 0.15058
1 2 0 0.99529
2 5 4 0.22488
2 4 4 0.95657
7 2 0 0.16679
0 5 2 0.66433
7 4 2 0.33499
meanZ1 = groupsummary(x, {'z2','z3'}, 'mean', 'z1');
meanZ1
12×4 table
z2 z3 GroupCount mean_z1
__ __ __________ _______
1 0 1 4
1 2 1 1
1 4 1 1
2 0 4 4.75
2 4 1 7
3 2 2 6.5
3 4 1 6
4 0 2 5.5
4 2 2 4.5
4 4 2 1.5
5 2 2 3.5
5 4 1 2
2 件のコメント
Ive J
2021 年 1 月 13 日
That is just an example! Of course you should not solely rely on it! It shows how to use groupsummary and apply it to your case. So, say you wanna group z2 and z3 and apply mean to z4 based on each group:
meanZ4 = groupsummary(x, {'z2','z3'}, 'mean', 'z4');
take a look at groupsummary
doc groupsummary
This is equivalent to getting indices for eahc unique values in z2 and z3:
% below is just an example for 0 and 2 in z2 and z3; loop over all possible combinations is not so neat! that's why groupsummary is preferred!
idx = x.z2 == 0 & x.z3 == 2; % table x row indices for z2 == 0 and z3 == 2
meanZ4_0_2 = mean(x.z4(idx));
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!