# Set Tolerance Distance when Interpolating with Nearest Value

15 views (last 30 days)
Ahmet Hakan UYANIK on 23 Nov 2022
Commented: William Rose on 24 Nov 2022
Hello everybody, I have latitude(100x1) longitude(100x1) and height(100x1) of a river. I need to create a grid out of it therefore i have used meshgrid with min(lat), min(lon), max(lat), max(lon) and have latitude, longitude in a square matrix(100x100) form . As a second stage i need to interpolate the heights for each grid and have heights in matrix(100x100). However every interpolation method fills the grids even if they are not even correlated according to distance. It interpolates the edges which I don't want. I would like to interpolate only the values in river(or around it). I m also okay if it just takes nearest value but not larger than 10meters for example griddata(latitude, longitude, height, MESH_lat, MESH_lon, distance=10.0) or F = scatteredInterpolant(latitude, longitude, height,'linear','nearest',radius,1.0) then I can do MESH_H = F(MESH_lat,MESH_lon);
Hope I am clear what intended to do. Thank you for the support.
##### 3 CommentsShow 1 older commentHide 1 older comment
William Rose on 23 Nov 2022
It is not clear to me what you want to do. Can you provide a small representative set of sample data? Instead of arrays with 82 million values in each, provide arrays with 30x40 points in each. Are the known heights located on a regular grid, and the locations where you want to interpolate irregularly spaced? Or vice versa? Do you want to create a set of points that are within a specified radius of a given point? If so, what is the location of the central point? Perhaps I will understand your question if you provide specific examples of each variable and array, of known heights and locaitons, and of desired query point locaitons. But don't let them be enormous, please.
Ahmet Hakan UYANIK on 23 Nov 2022
Edited: Ahmet Hakan UYANIK on 23 Nov 2022
as an example, i have latitude(100x1) longitude(100x1) and height(100x1) of a river. I need to create a grid out of it therefore i have used meshgrid with min(lat),min(lon),max(lat),max(lon) and have latitude, longitude in a square matrix(100x100) form . As a second stage i need to interpolate the heights for each grid and have heights in matrix(100x100). However every interpolation method fills the grids even if they are not even correlated according to distance. I want interpolate only the values in river. I m also okay if it just takes nearest value but not larger than 10meters for example. Hope i m clear enough with this explanation. Otherwise i can provide a code and sample data. Thanks (i have edited the main question to be more clear)

William Rose on 24 Nov 2022
Edited: William Rose on 24 Nov 2022
I assume you know the heights at scattered points, and you want to interpolate the heights to points on a grid, but you only want the grid to include points that are within radius R of a specified central point, c.
N=1000; %number of randomly scattered points
x=4*rand(N,1)-2; %N random x values in (-2,+2)
y=4*rand(N,1)-2; %N random y values in (-2,+2)
z=peaks(x,y); %height at each random point
F=scatteredInterpolant(x,y,z); %F=interpolant
xg=-2:.1:2; yg=-2:.1:2; %grid vectors
[X,Y]=meshgrid(xg,yg);
Zint=F(X,Y); %interpolated heights on the grid
c=[.35,.15]; %center point
xr=X(((X-c(1)).^2+(Y-c(2)).^2)<=R^2); %x values in region
yr=Y(((X-c(1)).^2+(Y-c(2)).^2)<=R^2); %y values in region
zr=F(xr,yr); %interpolated heights in the region
surf(X,Y,Zint,'EdgeColor','none'); %plot interpolated surface
xlabel('X'); ylabel('Y'); zlabel('Z'); hold on;
plot3(xr,yr,zr,'r*')
figure
surf(X,Y,Zint,'EdgeColor','none'); %plot interpolated surface
xlabel('X'); ylabel('Y'); zlabel('Z'); hold on;
plot3(xr,yr,zr,'r*')
axis equal; view(0,90) %view from above
Try it.
Ahmet Hakan UYANIK on 24 Nov 2022
Edited: Ahmet Hakan UYANIK on 24 Nov 2022
Thank you for the reply. It looks working well for a single center point but for a river, how am I gonna initialize a center point for each spatial location? because it has a curvature shape and has millions of observation.
[LIA,~]= ismembertol([X Y],[x,y],0.00001,'ByRows',true,'OutputAllIndices',true);
Zint(~LIA) = NaN;
Since dataset is so big, i am also trying to find a optimal solution.
William Rose on 24 Nov 2022
I think the GIS community, or GIS experts on this site, will have ideas for yhow to do this efficiently, but I do not.
It makes me thinkk of the 12 nautical mile and 234 nautical mile limits on charts, as seen below for the Florida Keys.
The GIS people have figured out waysa to do this efficiently.