Average of two dataset
조회 수: 50 (최근 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!