Find the Nearest Neighbor value in a Reference Grid
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to find a way to edit a loop which will allow me to assign any potential NaN values to their Nearest Neighbor within a reference grid (or domain).
My code creates 360 straight line transects away from a source location with an arc length of 1 degree, and uses the mapprofile function to pull out the range (rng) in degrees, depth (z) in metres at each latitude (lat) and longitude (lon) point. However, I noticed that for some transects there are NaN values for all outputs (lat/lon/range(rng)/depth(z)) at different points along the transects which I believe may be due to no exact/matching value existing within the reference grid at these points.
The domain consists of a grid of 2400x2800 pixels, for which each pixel has an associated latitude (lat), longitude (lon), reference matrix grid (R) and depth (Z) value. Unfortunately I cannot attach these due to their size.
Is there a way to ensure that any NaN values produced are assigned to their Nearest Neighbor values from the reference grid within the loops below?
if true
% Specify the source lat/long
sourcelat = 56.460723;
sourcelon = -5.29152;
% Create 360 transects
[endlat, endlon] = reckon(sourcelat, sourcelon, 1, 0:359, 'degrees');
% Create vectors for sourcelat/lon repeated 360 times
sourcelat = repmat(sourcelat,length(endlat),1);
sourcelon = repmat(sourcelon,length(endlon),1);
% Convert sourcelat/sourcelon to columns not rows
sourcelat = sourcelat';
sourcelon = sourcelon';
% Define the transects
plat = zeros(length(endlat),2);
plon = zeros(length(endlon),2);
% Create an array for depth values which has no specified length for
% the loop, 1 column in size
z_max = zeros(length(endlat),1);
% Loop outputs 360 values of plat/plon based on the sourcelat/sourcelon
% and endlat/endlon values defining the start and ends of the transects
% The loop then calculates the depth (z) for each range (rng) value at each lat
% and lon point
for k = 1:length(endlat)
% Calculate the transect lengths
plat(k,1:2) = [sourcelat(k) endlat(k)];
plon(k,1:2) = [sourcelon(k) endlon(k)];
% Output z_test values, rng, plat, plon
[z_test,~,~,~] = mapprofile(Z,R,plat(k,1:2),plon(k,1:2));
z_max(k)=length(z_test);
end
% Now initialise the array
z = NaN*ones(length(endlat), max(z_max));
rng = NaN*ones(length(endlat), max(z_max));
lat = NaN*ones(length(endlat), max(z_max));
lon = NaN*ones(length(endlat), max(z_max));
% Loop to calculate depth (z) for each range (rng)
for k = 1:length(endlat)
plat(k,1:2) = [sourcelat(k) endlat(k)];
plon(k,1:2) = [sourcelon(k) endlon(k)];
[z_test,~,~,~] = mapprofile(Z,R,plat(k,1:2),plon(k,1:2));
lz = length(z_test); % Calculates the length of each transect (as may vary)
[z(k, 1:lz),rng(k, 1:lz),lat(k, 1:lz),lon(k, 1:lz)] = mapprofile(Z,R,plat(k,1:2),plon(k,1:2));
end
end
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!