# Set Tolerance Distance when Interpolating with Nearest Value

7 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.
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.
##### 2 CommentsShowHide 1 older comment
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.