![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173393/image.jpeg)
[interp2] Different interpolation methods in each dimension.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
D. Plotnick
am 8 Mär. 2016
Bearbeitet: John D'Errico
am 9 Mär. 2016
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.
0 Kommentare
Akzeptierte Antwort
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173393/image.jpeg)
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
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.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!