MATLAB Answers

SS
0

No. of elements, mean and standard deviation of a field - conditions on other fields

Asked by SS
on 4 Oct 2019
Latest activity Commented on by SS
on 4 Oct 2019
Hi. I am working with a structure array S (1 X 50,000) with 10 fields.
For example,
Here is the input,
S(1).f1=[10,70,30 40,50,60], S(1).f2=[100,20,50,60,70,140] and S(1).f3=[-10,20,-50,42,-70,140] ;
S(2).f1=[16,98,74,47,99], S(2).f2=[101,54,69,20,11] and S(2).f3=[17,-54,69,-20,37];
S(3).f1=...... , S(3).f2=..... and S(3).f3=...........;
S(4).f1=.... , S(4).f2=..... and S(4).f3=............;
.
.
S(i).f1=...., S(i).f2=.... and S(i).f3=............;
Let's say, I have a main condition: 50 < f1 <=100. Based, on this condition I want to calculate few values
I want to know the number of elements in field f1 which satisfy the condition 50 < f1 <=100.
I want to know the number of elements in field f1 whose corresponding f3 > 0.
I want to know the number of elements in field f1 whose corresponding f3 < 0.
I want the mean and sd of f2 elements whose corrseponding f3 > 0.
I want the mean and sd of f2 elements whose corresponding f3 < 0.
Is there a simple way to find the above mentioned parameters?

  0 Comments

Sign in to comment.

1 Answer

Answer by meghannmarie on 4 Oct 2019
Edited by meghannmarie on 4 Oct 2019
 Accepted Answer

Maybe something like this?
f1 = [S.f1];
f2 = [S.f2];
f3 = [S.f3];
f1_cond1 = numel(f1(f1 > 50 & f1(f1 <=100)));
f2_idx = f3 > 0;
avg = mean(f2(f2_idx));
sd = std(f2(f2_idx));

  5 Comments

I tried this one before but, unfortunately it didn't give the correct answer. It considers all the elements of f1 whereas, I am looking for the specific elements.
Just want to mention that, we have two conditions, one on f1 (50 < f1 <=100) and in this range of f1 we want the number of elements in f3 (f3>0 and f3<0).
Is this what you mean?
idx1 = f1 > 50 & f1 <=100;
idx2 = f3 > 0;
idx = idx1 & idx2;
num1 = numel(f1(idx));
avg1 = mean(f2(idx));
sd1 = std(f2(idx));
idx2 = f3 < 0;
idx = idx1 & idx2;
num2 = numel(f1(idx));
avg2 = mean(f2(idx));
sd2 = std(f2(idx));

Sign in to comment.