Repeatedly interpolating from scattered to regular grid with varying sample values
7 views (last 30 days)
Show older comments
I want to do scattered interpolation in Matlab, but scatteredInterpolant does not do quite what I want.
scatteredInterpolant allows me to provide a set of input sampling positions and corresponding sample values. Then I can query the interpolated values by supplying a set of positions:
F = scatteredInterpolant(xpos, ypos, samplevals)
interpvals = F(xgrid, ygrid)
This is sort of the opposite of what I want. I already have a fixed set of sample positions, xpos/ypos, and output grid, xgrid/ygrid, and then I want to vary the sample values. The use case is that I have many quantities sampled at the same sampling positions, that should all be interpolated to the same output grid.
I have an idea how to do this for nearest neighbor and linear interpolation, but not for more general cases, in particular for natural neighbor interpolation.
This is what I want, in mock code:
G = myScatteredInterpolant(xpos, ypos, xgrid, ygrid, interp_method)
interpvals = G(samplevals)
In terms of what this means, I suppose G should hold a (presumably sparse) matrix of weights, W, and then G(samplevals) basically does W * samplevals, where the weights in the matrix W depends on the input and output grid, as well as the interpolation method (nearest neighbor, linear, natural neighbor). Calculating the matrix W is probably much more expensive than evaluating the product W * samplevals, which is why I want this to be reused.
Is there any code in Matlab, or in a similar language that I could adapt, that does this? Can it somehow be extracted from scatteredInterpolant in reasonable processing time?
0 Comments
Accepted Answer
Matt J
on 1 Jul 2020
Edited: Matt J
on 1 Jul 2020
You can change the Values property of the scatteredInterpolant object. That can eliminate significant object re-building time, as the following example shows.
[xgrid,ygrid]=ndgrid(linspace(0,1,500));
N=500^2;
tic;
F=scatteredInterpolant(rand(N,1), rand(N,1), rand(N,1));
toc;% Elapsed time is 1.048601 seconds.
newsamplevals=rand(N,1);
tic;
F.Values=newsamplevals;
toc;% Elapsed time is 0.398981 seconds.
tic;
F(xgrid,ygrid);
toc;% Elapsed time is 0.444320 seconds.
You could also consider generating the W matrix you are talking about using func2mat from the File Exchange
However, the construction of this matrix can be very slow, depending on the size of your grid. It has options to use the Parallel Computing Toolbox, if you have it, and this will mitigate computation time somewhat, but you would still have to assess whether the time to build W is worth the investment to you.
2 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!