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

 Réponse acceptée

Star Strider
Star Strider le 21 Jan 2022

0 votes

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 commentaires

aroj bhattarai
aroj bhattarai le 26 Jan 2022
Just another question: This extrpolation code greatly worked for the data set with continuous positive slope. What could be the trick if the dataset has a negative slope after a certain point, lets say at X = 0.4?
Thank you again.
Bhattarai
Star Strider
Star Strider le 26 Jan 2022
As always, my pleasure!
Since is the interpolation is with respect to ‘x’ and since ‘x’ is monotonically increasing, there should be no problems with the interpolation regardless of what ‘y’ does. (However if the interpolation was with respect to ‘y’ instead, the interpolation would need to be segmented into regions where ‘y’ was monotonically either increasing or decreasing, and then perform the interpolation on each segment.)
My apologies for the late reply — my day has been busier than I thought it would be.
aroj bhattarai
aroj bhattarai le 26 Jan 2022
Hi again,
I guess it should have been sraightforward as you have discussed. However, I got different result: the extrapolation is random and unusual. Nevertheless the averaging seem to be fine just before the extrapolation.
I do not know if it is okay to present the problem with a coplete excel file. But it would be great if you could look intot the attached dataset I attached here. My matlab code and the generated average result is also included in the excel sheet.
Thank you again for your great hint and effort.
Bhattarai
The data are interesting.
I am not certain what you want to do with them, since calculating and plotting the mean is straightforward —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/875160/Tran_Matlab.xlsx', 'VariableNamingRule','preserve')
T1 = 35×4 table
Var1 Var2 Var3 Var4 _____ ______ _____ ______ 1 0 1 0 1.025 0.0097 1.025 0.0094 1.05 0.0153 1.05 0.0177 1.075 0.0212 1.075 0.0297 1.1 0.0272 1.1 0.0464 1.125 0.0336 1.125 0.0682 1.15 0.0405 1.15 0.0949 1.175 0.048 1.175 0.1235 1.2 0.0562 1.2 0.1496 1.225 0.0656 1.225 0.1685 1.25 0.0758 1.25 0.2027 1.275 0.0877 1.275 0.2396 1.3 0.1016 1.3 0.2819 1.325 0.1177 1.325 0.323 1.35 0.1365 1.35 0.371 1.375 0.159 1.375 0.4188
plotMean = mean(T1{:,[2 4]},2);
figure
plot(T1{:,1}, T1{:,[2 4]})
hold on
plot(T1{:,1}, plotMean, '-g', 'LineWidth',1.5)
hold off
grid
xlabel('Var1, Var3')
legend('Var2', 'Var4', 'Mean', 'Location','NW')
.
aroj bhattarai
aroj bhattarai le 28 Jan 2022
Thank you S Strider, this seems to be more close to what we desire for.
Star Strider
Star Strider le 28 Jan 2022
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 21 Jan 2022

0 votes

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);

Community Treasure Hunt

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

Start Hunting!

Translated by