Average of two dataset
48 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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)
yy1 = interp1(x1, y1, x_avg, 'pchip','extrap');
yy2 = interp1(x2, y2, x_avg);
y_avg = mean([yy1; yy2], 1)
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 件のコメント
その他の回答 (1 件)
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);
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!