[interp2] Different interpolation methods in each dimension.

5 Ansichten (letzte 30 Tage)
I am looking for a way to use interp2 where for speed the interpolation method is different in each dimension. E.g. spline interpolation in column and linear in row. If possible I want to work within the existing framework, but since griddedInterpolant is a built-in method I am not sure this is possible. I can think of a number of instances where this capability in interp2 or interpn would be useful.

Akzeptierte Antwort

John D'Errico
John D'Errico am 9 Mär. 2016
Sorry, but while this could be faster IF you wrote it from scratch, it will probably not be so in practice, IF you tried to do it by some means. Anyway, you cannot tell interp2 to use different orders of interpolant in each dimension.
That does not mean it is impossible. It is in fact doable using spapi to create the interpolant.
x = -2:.5:2;
y=-1:.5:1;
[xx, yy] = ndgrid(x,y);
z = sin(xx+yy);
sp = spapi({2,4},{x,y},z);
fnplt(sp)
view(4,11)
That interpolant was linear in x, and cubic in y.
HOWEVER, note what appear to be scallops in the surface. These are indeed present in the interpolated surface, and are an artifact of interpolation. Note that those scallop artifacts are in fact considerably more obvious when a fully bilinear interpolant is applied to the same problem. This happens because a bilinear interpolant is not in fact quite as linear as you might think.
So, is this interpolant faster than interp2? Not so.
timeit(@() interp2(x,y,z',.15,.25,'spline'))
ans =
0.00049108
timeit(@() fnval(sp,[.15;.25]))
ans =
0.0013427
So for the spapi based spline interpolant that was linear in x and cubic in y, spapi was actually nearly 3 times slower than the full 2-d spline interp2 call.
Sorry. If you want significantly faster code, you would need to write it from scratch, and NOT in MATLAB.
  2 Kommentare
D. Plotnick
D. Plotnick am 9 Mär. 2016
Bearbeitet: D. Plotnick am 9 Mär. 2016
Thanks for your quick reply. In general this answers my question, but does bring up (many) more questions. Near the top is; is the relative speed boost between spapi and interp2 purely an implementation difference? I do not appear to gain any speed in spapi based on whether I am using bilinear, linear-cubic, or any other combination. For example:
% your code
x = -2:.5:2;
y=-1:.5:1;
[xx, yy] = ndgrid(x,y);
z = sin(xx+yy);
% test
sp = spapi({2,2},{x,y},z);
timeit(@() fnval(sp,[.15;.25]))
sp = spapi({2,4},{x,y},z);
timeit(@() fnval(sp,[.15;.25]))
sp = spapi({4,4},{x,y},z);
timeit(@() fnval(sp,[.15;.25]))
There does not appear to be a significant speed advantage to using fewer knots, which to me points to an implementation difference.
That being said, I do think it is likely I will have to build my own interpolation algorithm to do this.
Thanks again.
SIDE QUESTION - This is my first time trying this on the Matlab answers site, how do you get your code to evaluate as above inside of the comment section? Did you just run them locally and upload a published file?
John D'Errico
John D'Errico am 9 Mär. 2016
Bearbeitet: John D'Errico am 9 Mär. 2016
To bring in results, just run your code in MATLAB, then copy and paste from the command window. Don't forget to identify it as code when you do.
(In my tests, spapi was a speed loss compared to interp2, not a boost of course.)
More or less knots would not materially affect the evaluation speed of a spline generated by spapi. All the "hard" work was done in the call to spapi, so that call should be time dependent on the number of points in each dimension. The problem is small enough that changing the number of points of the order of the spline seems not to significantly affect the time.
A quick test of the code for a much denser grid shows there is a difference, but not a huge one.
x = -2:.5:2;
y = -1:.5:1;
[xx, yy] = ndgrid(x,y);
z = sin(xx+yy);
timeit(@() spapi({2,2},{x,y},z))
ans =
0.0020386
timeit(@() spapi({6,6},{x,y},z))
ans =
0.002059
x = -2:.01:2;
y = -1:.01:1;
[xx, yy] = ndgrid(x,y);
z = sin(xx+yy);
timeit(@() spapi({2,2},{x,y},z))
ans =
0.042453
timeit(@() spapi({6,6},{x,y},z))
ans =
0.067521
The difference is not immense though. So spapi probably has a fair amount of overhead.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by