Mean and standard deviation of a field - condition on another field
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
更多回答(1 个)
the cyclist
2019-8-30
编辑:the cyclist
2019-8-30
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.
3 个评论
the cyclist
2019-8-31
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!