MATLAB Answers


Mean and standard deviation of a field - condition on another field

Asked by SS
on 30 Aug 2019
Latest activity Commented on by SS
on 4 Oct 2019 at 11:11
Hi. I am working with a structure array S (1 X 50,000) with 10 fields. I want to calculate the mean and standard deviation of field f1 based on a condition on field f2.
For example,
Here is the input,
S(1).f1=[10,20,30 40,50,60] and S(1).f2=[100,20,50,60,70,140];
S(2).f1=[56,98,74,87,99] and S(2).f2=[101,54,69,20,11]
S(3).f1=...... and S(3).f2=.....
S(4).f1=.... and S(4).f2=.....
S(i).f1=.... and S(i).f2=....
I want to calculate the mean of f1 elements whose corresponding f2 field values are 50 < f2 <=100
Meaning, S(1).f1=[10,40,50] and S(2).f1=[98,74] should be considered in calculation of mean.
I want to calculate several mean values of f1 based on different conditions on f2.


Sign in to comment.

2 Answers

Answer by Matt J
on 31 Aug 2019
Edited by Matt J
on 31 Aug 2019
 Accepted Answer

How can I get one single (explicit) mean and standard deviation?
F=F1(F2>50 & F2<=100);


Show 1 older comment
How can I know how many data points/field values are considered in calculation of the mean? Or in other words, how many data points were there based on the above condition?

Sign in to comment.

Answer by the cyclist
on 30 Aug 2019
Edited by the cyclist
on 30 Aug 2019

The mean and standard deviation of f1, for the elements where 50 < f2 <= 100, can be calculated as follows:
arrayfun(@(x)mean(x.f1(x.f2>50 & x.f2<=100)),S);
arrayfun(@(x) std(x.f1(x.f2>50 & x.f2<=100)),S);
arrayfun will apply a function to each element of a structure array, so it is a vectorized way of doing the operation.


I suggest taking a look at the documentation. The first input to arrayfun is the function to apply. So, the use of "x" here is similar to how one would say "f(x) is a function" is mathematics. x is just a placeholder variable, not one of your actual variables. In place of x, MATLAB will use S(1), then S(2), and so on.
You might want to take a look at the documentation for anonymous functions, too.
Thanks for the reply. I have tried using the above lines but, unfortunately it creates another structure of size 1 X N in which, some entries are numeric and others are NaN. I have used mean(new created structure) to see whether, it will give one single answer but, it gives NaN as the answer.
How can I get one single (explicit) mean and standard deviation?
For a length-L structure, I thought you wanted the L means and standard deviations, one for each element in the structure array. The reason there were some NaNs is presumably that sometime f2 didn't have any elements that fit the condition.
Looks like Matt J got you what you wanted.

Sign in to comment.