Table Mean and Standard Deviation
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
WalterWhite
el 13 de En. de 2021
you can also import the table as coloumn vectors and perform the logical indexing
Respuestas (1)
Ive J
el 13 de En. de 2021
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 comentarios
Ive J
el 13 de En. de 2021
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));
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!