MATLAB: Using knnsearch to find nearest values ignoring NaN’s

indicesinterp2knnsearchlatitudelongitudenanvalues

Hello,
I have two datasets. A is a high resolution global dataset, and B is a smaller dataset with sparse points. I want to find the indices of lat and lon in A that are closest to B lat and lon, but I want to ignore the NaN values in A and instead get the next closest value that isnt NaN.
Is there any way to do this?
A is a 2D dataset (latxlon) while B is three individual vectors of lat, lon, and data Which is why it makes it hard to use a lot of other commands.
Please let me know if you have any ideas,
Thanks,
Melissa

Best Answer

You need to organise the nearest neighbour search so that it uses both latitude and longitude together, not search each separately. Try this, assuming you want to use Euclidean distance in geographical coordinates:
[rowA, colA] = find(~isnan(A));
lon_A = lon(colA); % I assume lon and lat convert from index to degrees
lat_A = lat(rowA);
idx = knnsearch([lon_A lat_A], [lon_B(:) lat_B(:)]);
nearest_lat = lat_a(idx);
nearest_lon = lon_a(idx);
nearest_Avalue = A(sub2ind(size(A), rowA(idx), colA(idx)));
I haven't checked this as I don't have your data or the statistics toolbox, but if it doesn't work please say what goes wrong in a comment.
[Edit: inserted call to sub2ind to correctly get nearest_Avalue.]