interpn griddata difference?
显示 更早的评论
Hi,
What are the differences between interpn and griddata?
Thank you.
采纳的回答
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 个评论
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);
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
Thanks for all your help. You have been really helpful! Stay safe.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
