Average of two dataset
48 views (last 30 days)
Show older comments
aroj bhattarai
on 21 Jan 2022
Commented: Star Strider
on 28 Jan 2022
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 Comments
Accepted Answer
Star Strider
on 21 Jan 2022
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 Comments
More Answers (1)
Image Analyst
on 21 Jan 2022
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 Comments
See Also
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!