How can I make automatic calculation for averages for given ranges?

1 次查看(过去 30 天)
I have 4 case with different x and y values. I want to combine these 4 cases. When doing this combination, if there are some x values in common then I want to take average. Basically, there are 4 case and I want to combine them according to their inputs ( x ). First A, x is between 0.0703-0.1771 Second B, x is between 0.0937-0.2361 Third C, x is between 0.1406-0.3541 Fourth D, x is between 0.2812-0.7083.
I want to calculate y values in average. If there are some x values in use for 2 or 3 case (i.e. 0.1505 is in A, B and C) to take average I want to divide by number of case that include this value. For example, x from 0.0703 to 0.0936, there is only one case, then no need to take average.
Thank you in advance.
  7 个评论
Murat Yetkin
Murat Yetkin 2017-12-12
let me give an example: first set
x11=0.07, x12=0.08 x13=0.09 x14=0.10
y11=2.1, y12=2.4, y13=2.8, y14=3.2;
second set
x21=0.09, x22=0.10, x23=0.11, x24=0.12 y21=3, y22=3.3, y23=3.7, y24=4.1
when I combine these two: for 0.07,0.08,0.11 and 0.12 there are only 1 value for each for 0.09 and 0.10 there 2 values.
for single values, we can use them directly but for others we need to take average. it means combination of them should be like that:
0.07 >>>>>>>>>2.1
0.08 >>>>>>>>>2.4
0.09 >>>>>>>>>(2.8+3)/2
0.10 >>>>>>>>>(3.2+3.3)/2
0.11 >>>>>>>>> 3.7
0.12>>>>>>>>>>4.1
Image Analyst
Image Analyst 2017-12-12
Not sure I follow. Hopefully you have arrays and not a bunch of separately named variables, like y24 is really y(2,4). Anyway, check out ismembertol() and see if it does what you want.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2017-12-10
Try this:
range1 = x >= 0.0703 & x <= 0.1771
range2 = x >=0.0937 & x <=0.2361
range3 = x >= 0.1406 & x <=0.3541
range4 = x >= 0.2812 & x <=0.7083
sumA = sum(A(range1));
sumB = sum(B(range2));
sumC = sum(C(range3));
sumD = sum(D(range4));
numTerms = sum(range1+range2+range3+range4)
overallMean = (sumA + sumB + sumC + sumD) / numTerms

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by