Assigning values from small grid into larger grid using nearest coordinates

1 view (last 30 days)
I am trying to assign new values of depth from a smaller cropped grid (480 x 480 - lon, lat, depth) into a larger grid (4800 x 5760 - lon, lat, depth) using the nearest coordinates from both grids.
I have so far found the indexes of latitude and longitude which are within the larger grid, but I am struggling to assign the new bathymetry values at these index points, and keep the bathymetry values the same outside of these index points.
Would anyone be able to help me?
lon_new = unique(lon_smallgrid); % unique longitude values from the small grid (1800 x 1)
lat_new = unique(lat_smallgrid); % unique latitude values from the small grid (1800 x 1)
lon_orig = unique(lon.largegrid); % unique longitude values from the large grid (4800 x 1)
lat_orig = unique(lat.largegrid); % unique latitude values from the large grid (5760 x 1)
[ilon] = nearestpoint(lon_new, lon_orig); % value of lon_new which is closest to each value in lon_orig (1800 x 1)
[ilat] = nearestpoint(lat_new, lat_orig); % value of lat_new which is closest to each value in lat_orig (1800 x 1)

Answers (1)

KSSV
KSSV on 16 Apr 2019
Read about knnsearch. This will give you indices and you can easily replace the values.
  1 Comment
Charlotte Findlay
Charlotte Findlay on 16 Apr 2019
Edited: Charlotte Findlay on 16 Apr 2019
Hi KSSV,
So knnsearch just gives me the same output as nearestpoint.
I found a way to input the data directly into the larger grid (depth) if the max(ilat/ilon) - min(ilat/ilon) is equal to the size of the small grid (see code below).
depth((min(min(ilon)):max(max(ilon))-1), (min(min(ilat)):max(max(ilat))-1)) = small_grid;
But now I'm stuck again as some of my small grids have different dimensions to the max(ilat/ilon) - min(ilat/ilon).
Does anyone have any ideas how to deal with this?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!