Average of two dataset

조회 수: 50 (최근 30일)
aroj bhattarai
aroj bhattarai 2022년 1월 21일
댓글: Star Strider 2022년 1월 28일
Dear all,
I have two experimental data set of different ranges from 0 to 0.5 and 0.6:
% Test data:
x1 = linspace(0, 0.5, 10);
y1 = 2.0.*x1.^2;
x2 = linspace(0, 0.6, 10);
y2 = 3.5*x1.^2;
To create an average data set from such input data, it seems interpolation could be an option:
% Average:
x_avg = linspace(0, 0.6, 10);
yy1 = interp1(x1, y1, x_avg);
yy2 = interp1(x2, y2, x_avg);
y_avg = mean([yy1; yy2], 1);
However, with the above interpolation script, the final average data set is limited to x_avg = 0.466667. Is it possible to get the average data until x_avg = 0.6 in such condition in Matlab? Or there is other way to solve the problem?
Thank you very much in advance.
Bhattarai

채택된 답변

Star Strider
Star Strider 2022년 1월 21일
I cannot reproduce that problem here (R2021b).
However, to compare all values for both curves, the (x1,y1) values need to be extrapolated (otherwise the values beyond 0.5 are NaN) for ‘yy1’ and ‘y_avg’.
% Test data:
x1 = linspace(0, 0.5, 10);
y1 = 2.0.*x1.^2;
x2 = linspace(0, 0.6, 10);
y2 = 3.5*x1.^2;
% Average:
x_avg = linspace(0, 0.6, 10)
x_avg = 1×10
0 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000
yy1 = interp1(x1, y1, x_avg, 'pchip','extrap');
yy2 = interp1(x2, y2, x_avg);
y_avg = mean([yy1; yy2], 1)
y_avg = 1×10
0 0.0097 0.0393 0.0886 0.1576 0.2461 0.3544 0.4824 0.6300 0.7957
figure
subplot(2,1,1)
plot(x1, y1, '.-')
hold on
plot(x2, y2, '.-')
hold off
grid
subplot(2,1,2)
plot(x_avg, yy1, '.-')
hold on
plot(x_avg, yy2, '.-')
plot(x_avg, y_avg, '.-')
hold off
grid
I included the extrapolation here. Remove it if that is not the intended result.
.
  댓글 수: 6
aroj bhattarai
aroj bhattarai 2022년 1월 28일
Thank you S Strider, this seems to be more close to what we desire for.
Star Strider
Star Strider 2022년 1월 28일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 1월 21일
Try it this way:
% Test data:
x1 = linspace(0, 0.5, 10)
y1 = 2.0.*x1.^2
x2 = linspace(0, 0.6, 10)
y2 = 3.5*x1.^2
% Create new x samples from all available x
xBoth = sort([x1, x2]);
% Interpolate both y at these new sample points.
% Where xBoth is outside the range of the original x
% the values will be nan.
yy1 = interp1(x1, y1, xBoth);
yy2 = interp1(x2, y2, xBoth);
% Stack the interpolated values vertically and
% take the mean going down rows but ignoring nans.
y_avg = mean([yy1; yy2], 1);

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by