How to I generate continuous arrays from two arrays with different data points?

10 次查看(过去 30 天)
I would like to take two different array data sets:
e.g.
x1 = [ 1 5 8 20 ];
y1 = [10 12 15 20];
x1 = [2 6 8 19 21];
y1 = [1.5 5.3 16 22];
I would like to take these and turn the y values into continuous arrays along the same x array (say x = 0:1:22). That way I can find approximately what measurements of y1 and y2 are at for say x=2 and do things such as calculate the percent difference. Please let me know if this is not enough clarification.
Thank you!

回答(1 个)

the cyclist
the cyclist 2023-8-2
编辑:the cyclist 2023-8-2
Your question is not perfectly clear to me, but it sounds like you could use the interp1 function to interpolate each data set to a common set of x points.
x1 = [ 1 5 8 20 ];
y1 = [10 12 15 20];
x2 = [2 6 8 19 21];
y2 = [1.5 5.3 16 22 23]; % I changed this, because yours were not the same length
xq = 0:22;
y1q = interp1(x1,y1,xq)
y1q = 1×23
NaN 10.0000 10.5000 11.0000 11.5000 12.0000 13.0000 14.0000 15.0000 15.4167 15.8333 16.2500 16.6667 17.0833 17.5000 17.9167 18.3333 18.7500 19.1667 19.5833 20.0000 NaN NaN
y2q = interp1(x2,y2,xq)
y2q = 1×23
NaN NaN 1.5000 2.4500 3.4000 4.3500 5.3000 10.6500 16.0000 16.5455 17.0909 17.6364 18.1818 18.7273 19.2727 19.8182 20.3636 20.9091 21.4545 22.0000 22.5000 23.0000 NaN
There are NaN values because interp1 will not extrapolate by default. See the documentation for options.

类别

Help CenterFile Exchange 中查找有关 Data Preprocessing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by