Faster interpolation than interp2 ?
26 次查看(过去 30 天)
显示 更早的评论
Hey guys, I have a question. I am currently using interp2 for interpolation of single points on a grid. In my case it is
if true
k1u=interp2(lat,lon,u_now_flow,b,a)
end
So lat and lon are the sample grid points (this is how matlab calls it. they get created by meshgrid), u_now_flow is a flow field and b and a are vectors which look like this:
53.9603
53.9603
53.9603
53.9703
53.9703
53.9703
53.9803
53.9803
53.9803
54.0003
so the code is working fine, but I wanted to know if there are faster ways/functions to interpolate the points on a grid. I tried to use griddedInterpolant, but failed cause I always got NaN, but I have no idea why. I hope you can help me
0 个评论
采纳的回答
Guillaume
2016-12-5
griddedInterpolant may be marginally faster than interp2 since interp2 does a little bit of argument manipulation before finally using griddedInterpolant to do the heavy work. I wouldn't expect the difference to be significant enough that it'd matter.
There are no other interpolating methods in matlab that are faster.
Using griddedInterpolant, your code should be:
ginterp = griddedInterpolant(lat, lon, u_now);
k1u = ginterp(a, b); %assuming a and b are vectors of the same length and shape.
2 个评论
Guillaume
2016-12-6
I'm not sure what that discussion in the doc of griddedInterpolant about meshgrid vs ndgrid is about. I doesn't matter which you use to generate the grid, as long as it's consistent with the data. In any case, i would always use ndgrid since it does not mix up two different coordinates systems.
Your first issue is indeed with the fact that your lat vector is strictly monotically decreasing instead of strictly monotically increasing. That is easily fixed, flip it up/down and, of course, do the same with the velocity map.
lat = flipud(lat);
u_now_flow = flipud(u_now_flow); %to reflect the ordering change on lat
Now, you can ndgrid your latitude, longitude and pass that to griddedInterpolant, if you wish:
[glat, glon] = ndgrid(lat, lon); %after lat has been flipped
interpolant = griddedInterpolant(glat, glon, u_now_flow); %after u_now_flow has been flipped
but you don't even need to grid the coordinates, if you use this syntax:
interpolant = griddedInterpolant({lat, lon}, u_now_flow); %after flipping
Demo code:
lat = (54.375 - (0:36)*0.05) .'
lon = (5.0417 + (0:22)*0.0533) .'
u_now_flow = exp(-bsxfun(@plus, (lat - 53.5).^2, (lon.' - 6).^2)); %nice surface for demo
interpolant = griddedInterpolant({flipud(lat), lon), flipud(u_now_flow));
interpolant(53.9603, 7.7076)
Note the order of inputs for the interpolant is latitude, longitude. Otherwise of course you get NaN since the coordinates are outside the grid.
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!