Mean based on positive and negative values of a field

8 visualizaciones (últimos 30 días)
SS
SS el 29 de Ag. de 2019
Comentada: SS el 30 de Ag. de 2019
I have a structure S (10000 X 1) with 10 fields. I want to calculate (two) means and standard deviations of one particluar field f1. I have condition on field f2.
mean_1 and SD_1: consider the values of f1 (in calculation of mean and SD) if, f2 is positive.
mean_2 and SD_2: consider the values of f1 (in calculation of mean and SD) if, f2 is negative.
Any smart way to do this?

Respuesta aceptada

Rik
Rik el 29 de Ag. de 2019
Logical indexing:
L=[S.f2]>0;
pos_mean=mean([S( L).f1]);
neg_mean=mean([S(~L).f1]);
  7 comentarios
Rik
Rik el 30 de Ag. de 2019
As I mentioned, non-scalar structs complicate it a bit.
all_f1=[S.f1];
all_f2=[S.f2];
L=all_f2>0;
pos_mean=mean(all_f1( L));
neg_mean=mean(all_f1(~L));
Note that the concatenation will probably not work if your fields have a different direction than your struct array. So this should work, but a 100X1 struct array with 1Xn fields will not.
SS
SS el 30 de Ag. de 2019
Great, thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures 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!

Translated by