Interpolate consecutive values of a single array
18 次查看(过去 30 天)
显示 更早的评论
Hi guys!
I have two datasets whose arrays have not the same length. The first contains Beta values and it counts 100 values; the second are temperature data and it contains 7985 values. What I would like to do is a linear interpolation between two consecutive data values from beta, in order to get the same length of temperature array.
So, if temperature values are 7985, I would require 79.85 linear interpolated beta elements from beta1 to beta2, then 79.85 linear interpolated elements from beta2 to beta3, and so on ...
In the end I would get a 7985 beta array I will be able to scatterplot Vs. 7985 temperature array data.
Is it possible to easly create such a script/function? I am trying using linspace function, but I cannot make it consecutive value after value.
Thanks in advance for your patience and help :)
2 个评论
Sargondjani
2022-11-16
Have a look at griddedInterpolant.
x = 1:100;
intBeta = griddedInterpolant(x,Beta,'linear')
Now you can linearly interpolate between beta values: intBeta(1.5) should give you the Beta value between observation 1 and 2.
So next you would need to construct a vector where you retrieve the interpolated beta values. there's examples in the function description.
采纳的回答
William Rose
2022-11-17
I assume that the values in beta are not a simple sequence such as beta=1:100. If that were tru, this would be easy.
I will illustrate a solution by example:
%You will get 100 values for beta from a file.
%Since I don't have the file, I will create 100 values.
beta=-1*(-1:.02:.98)+(-1:.02:.98).^3; %100 beta values
t=1:length(beta); %values to associate with beta, to use for interpolation
%You will get 7985 temperature values from a file.
%Since I don't have the file, I will create 7985 values.
N=7985;
temp=20+10*sin(2*pi*(1:N)/2000); %7985 temperature values
Plot the data:
subplot(311); plot(1:100,beta,'-bx'); ylabel('\beta');
subplot(312); plot(1:7985,temp,'-r'); ylabel('Temperature');
Interpolate beta:
t1=linspace(t(1),t(end),N); %create a vector of 7985 "time" values
beta1=interp1(t,beta,t1,'linear'); %interpolate linearly between beta values
Plot temperature versus interpolated beta:
subplot(313); scatter(beta1,temp,'k');
xlabel('\beta'); ylabel('Temperature'); grid on
Try it with your data. Good luck.
2 个评论
William Rose
2022-11-17
beta_spacing, defined as you have defined it, draws a straight line from the first to the last point of beta. If the first and last points are zero, then beta_spacing will be a long vector of zeros, which is not useful. Even if the first and last values are not the same, beta_spacing will not track the fluctuations in beta, so it is not useful for interpolation. By creating a time vector, t=1:100, we can associate each beta with a time. Then we can use the time vector to interpolate from the known beta values at the known times, to intermediate values of beta at intermediate times.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
