1D interpolation with non-monotonic scattered data

조회 수: 10 (최근 30일)
Kim
Kim 2014년 10월 20일
답변: AlexL 2022년 7월 19일
I cant figure out how do 1D interpolation with scattered data. I know how to get it working with 2D interpolation, but scatteredinterpolant only works with 2D or more. Thank you for your help!

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 10월 20일
편집: Andrei Bobrov 2014년 10월 20일
small example:
x = rand(30,2); % you data
x1 = sortrows(x,1);
F = griddedInterpolant(x1(:,1),x1(:,2));
out = F(xinput);
  댓글 수: 2
Kim
Kim 2014년 10월 20일
I just tried it and I got the following message: Error using griddedInterpolant The grid vectors are not strictly monotonic increasing.
Andrei Bobrov
Andrei Bobrov 2014년 10월 20일
please see small example in my answer

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

추가 답변 (2개)

Riley
Riley 2016년 10월 5일
Just ran into this same issue, however the solution posted seems to avoid tackling an important detail here- griddedInterpolant, unlike scatteredInterpolant, doesn't remove repeated index points (at least, on 2015a). For example:
x = rand(30,2); % clean data
x1 = sortrows(x,1);
F = griddedInterpolant(x1(:,1),x1(:,2)); % this is fine
x_repeat = x1(:,1);x_repeat(2) = x_repeat(3); % introduce a repeated index point
F = griddedInterpolant(x_repeat,x1(:,2)); %produces an error
compare that to the scatteredInterpolant class-
x = rand(30,3); % clean data
x1 = sortrows(x,1);
x_repeat = x1(:,1:2);x_repeat(2,:) = x_repeat(3,:); % introduce a repeated index point
F = scatteredInterpolant(x_repeat,x1(:,3)); %rather than throwing an error, shows a warning and cleans your data for you
however, as scatteredInterpolant requires at least 2 dimensions for its indices, this doesn't work for 1d interpolation. Clearly at this point you can add your own cleaning method, but if you are using this class chances are you are trying to avoid writing that sort of code in the first place. As such, here's a hack I used to use the scatteredInterpolant cleaning method on a 1d data set-
xi = rand(30,1);%indices
x = sort(xi);
v = rand(30,1);% values
xq = [0.5 0.6];% query indices
x(2) = x(3);% the repeated index
Fs = scatteredInterpolant(x*ones(1,2),v);%make 1d into 2d
%note that the sane thing to do doesn't work: vq = Fs(xq'*ones(1,2)) produces an empty vector
F = griddedInterpolant(Fs.Points(:,1),Fs.Values);
vq = F(xq);
this produces plenty of warnings and will make you feel bad, but if you want a quick solution it will work

AlexL
AlexL 2022년 7월 19일
I was also curious to solve this issue.
I wrote a short function that was helpful for me. You're welcome to try it (attached).

카테고리

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