Interpolate consecutive values of a single array

조회 수: 12 (최근 30일)
Luca Castrogiovanni
Luca Castrogiovanni 2022년 11월 16일
답변: Luca Castrogiovanni 2022년 11월 17일
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
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.
Luca Castrogiovanni
Luca Castrogiovanni 2022년 11월 17일
Thank you Sargondjani :)

댓글을 달려면 로그인하십시오.

채택된 답변

William Rose
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
Luca Castrogiovanni
Luca Castrogiovanni 2022년 11월 17일
Thank you William and thank you all for your help! It actually worked with this code. I did not think about replacing interp query with a linspace.
Why do I need to specify t = 1:length(beta)? I tried directly typing:
beta_spacing = linspace(beta(1),beta(end),7985);
beta_interp = interp1(numel(beta),beta,beta_spacing,'linear')
but of course it did not work. Just to figure out why t variable is so important. Maybe interp1 function need to knwo the number of values further than values itself in the array?
Anyway it worked. Thank you again for your help!
William Rose
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개)

Luca Castrogiovanni
Luca Castrogiovanni 2022년 11월 17일
Great! Many thanks :)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by