1D interpolation with non-monotonic scattered data
28 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Andrei Bobrov
on 20 Oct 2014
Edited: Andrei Bobrov
on 20 Oct 2014
small example:
x = rand(30,2); % you data
x1 = sortrows(x,1);
F = griddedInterpolant(x1(:,1),x1(:,2));
out = F(xinput);
2 Comments
More Answers (2)
Riley
on 5 Oct 2016
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
0 Comments
AlexL
on 19 Jul 2022
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).
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!