Interpolation using scattered query points

Hi
It is straight forward to use interpn or griddedInterpolant to perform n-dim interpolation. However, in these functions, the query points must be on a full grid. Is it possible to perform interpolation on a scattered set of query points?
As an example, for 2D interpolation, I don't want to perform interpolation on a full xy grid. I just want to perform interpolation on a set of points {(x1,y1), (x2,y2), (x3,y3), ...}. I know it can be done using a loop, but I was looking for a faster and more efficient vectorized solution.
The question at the link below is similar to my question, but the answer didn't give a vectorized solution.
Thanks

 Accepted Answer

I believe the solution you point to can be trivially vectorized:
load clown ;
imagesc(X);
colormap gray ;
[nx,ny] = size(X) ;
r = 1:ny ;
c = 1:nx ;
[R,C] = meshgrid(r,c) ;
%%do interpolation
Ri = [5 6]; Ci = [5 7]; % <------- I VECTORIZED THE CODE RIGHT HERE.
Xi1 = interp2(R,C,X,Ri,Ci) ;
% or can be used without mesh grid like below
Xi2 = interp2(r,c,X,Ri,Ci) ;

4 Comments

Hesham
Hesham on 9 Aug 2019
Edited: Hesham on 9 Aug 2019
Thanks for your answer. But the code you provided will actually interpolate on the full grid, i.e., it will yield the result for 4 query points (5,5) (5,7) (6,5) (6,7).
What I actually need is similar to MATLAB's element wise operations. I just need the first element in Ri with first element in Ci, the 2nd with the 2nd, and so on, i.e., (5,5) and (6,7) only.
For two data points the question seems trivial, but I actually need to perform interpolation on a set of thousand specific query points that are not on a grid. Again, a loop will do the job, but I was looking for a faster solution.
Here is a sample of a code that does NOT do what I need. I just need (1,3) and (2,4).
[x,y] = ndgrid(0:1:10);
z = x.^2 + y.^2;
F = griddedInterpolant(x,y,z);
xq = [1 2];
yq = [3 4];
vq = F({xq,yq'})
vq =
10 17
13 20
Hm. For me, the output of the code I posted yields two points.
Xi1 = [61 55];
I wonder if it is a version issue? I am using R2019a.
It turned out that the answer is quite simple! The code below works as I need.
@the cyclist: Many thanks for pointing out the solution is trivial!
[x,y] = ndgrid(0:1:10);
z = x.^2 + y.^2;
F = griddedInterpolant(x,y,z);
vq = F([1 3; 2 4])

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2017a

Asked:

on 9 Aug 2019

Commented:

on 9 Aug 2019

Community Treasure Hunt

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

Start Hunting!