Réponse acceptée

Bjorn Gustavsson
Bjorn Gustavsson le 14 Avr 2020

0 votes

The interpn function requires input data on a plaid grid, i.e. as you would produce with meshgrid, or perhaps ndgrid, while griddata (and scatteredInterpolant and TriScatteredInterp) expects input data on irregular grids. GRIDDATA expects the inputs as 1-D vectors. See the help and documentation for the respective functions, there you should find "minimal" illustrating examples to look at.
HTH

5 commentaires

Tahariet Sharon
Tahariet Sharon le 15 Avr 2020
Thanks. Do I understand correctly that interpn can be used also for the cases where griddata is used (for irregular grids)?
No. interpn can only handle grids as produced by ndgrid (I recalled that it could also handle grids as produced by meshgrid, but that was not the case for the matlab-version on my laptop, this might have been the case for "very" old matlab-versions.). That means that the grids for interpn might have step-sizes that varies along each separate dimension:
[x,y,z] = ndgrid([0 1 2],[-3 -1 0 1 3],[0 1 2 4 8]);
[xi,yi,zi] = meshgrid(0:.1:2,-3:.1:3,0:.1:8);
I = exp(-(x-1).^2-(y-1).^2-(z-3).^2);
Ii = interpn(x,y,z,I,xi,yi,zi);
slice(xi,yi,zi,Ii,1,1,3),shading flat
That grid is still "plaid" - Each "cell" [dx,dy,dz] would still be cube-ish (dont remember name of that geometric shape in English). griddata and the other triangulation-based interpolation-functions handle interpolation of properly irregularly sampled data:
xR = 1 + randn(75,1);
yR = 2*randn(75,1);
zR = 4 + 2.5*randn(75,1);
Ir = exp(-(xR-1).^2-(yR-1).^2-(zR-3).^2);
Igd = xi;
Igd(:) = griddatan([xR,yR,zR],Ir,[xi(:),yi(:),zi(:)]);
slice(xi,yi,zi,Igd,1,1,3),shading flat
hold on
scatter3(xR,yR,zR,32,log(Ir),'filled')
That type of data interpn cannot handle.
HTH
Actually, when I run the example above I get:
Warning: Query data is in MESHGRID format, NDGRID format will yield better performance.
Convert your query data (Xq, Yq, Zq) as follows:
F = griddedInterpolant(. . .);
P = [2 1 3];
Xq = permute(Xq, P);
Yq = permute(Yq, P);
Zq = permute(Zq, P);
Vq = F(Xq,Yq,Zq);
Bjorn Gustavsson
Bjorn Gustavsson le 16 Avr 2020
Yeah, that might very well be a slight performance-booster (check such things with the profiler-functionality if you need speed-ups...). I was too lazy to do that since my example was rather small and I only wanted to plot the data with the slice function, which used to expect the data to be in the meshgrid format.
The one advice when it comes to this meshgrid/ndgrid option is DOCUMENT YOUR CHOICE CLEARLY (and I'm shouting mainly for me to remember this...), the speed-up should be rather minor - typically, I think I've lost more time hunting down where I've done which choice than I've gained or lost due to this performance difference.
HTH
Tahariet Sharon
Tahariet Sharon le 17 Avr 2020
Thanks for all your help. You have been really helpful! Stay safe.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by