Table Mean and Standard Deviation

조회 수: 2 (최근 30일)
Lisa Klausthaler
Lisa Klausthaler 2021년 1월 13일
댓글: Ive J 2021년 1월 13일
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
WalterWhite 2021년 1월 13일
you can also import the table as coloumn vectors and perform the logical indexing

댓글을 달려면 로그인하십시오.

답변 (1개)

Ive J
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
Lisa Klausthaler
Lisa Klausthaler 2021년 1월 13일
First of all thank you very much for your time and help! However, I dont quite get this. Head(table) does not change anything in my data, hence in your table the values are mixed from 0-7 (which is not quite correct, becuase z1 just equals the trial numbers 1,2,3,4,5,6,7 etc.)
And then the mean in second file should just be calculated for Z4, so the numbers have to lay between 0 and 1. But I want to calculate, e.g. all conditions that fullfill z2 = 0 and z3=2, which could be 0.7 or something in z4. Do you get my point?
Ive J
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by