How to find the corresponding points of interest on a lat/lon matrix?

12 views (last 30 days)
I have latitude (updownLat) and longitude (updownLon) coordinates for my points of interest.
I have a lon x lat grid of size 1440x121 covering my domain.
I have a 1440x121 matrix for the latitude at each grid cell (latMat).
I have a 1440x121 matrix for the longitude at each grid cell (lonMat).
I have 1440x121 matrix with pressure values at each grid cell (mslp1).
I want this:
1) Matrix of size 1440x121 with the latitude values only at the coordinates of the points of interest and NaN elsewhere (latMatMask);
2) Matrix of size 1440x121 with the longitude values only at the coordinates of the points of interest and NaN elsewhere (lonMatMask);
3) Matrix of size 1440x121 with the pressure values only at the coordinates of the points of interest and NaN elsewhere (mslp1Mask);
4) Vector of length(updownLat) with the pressure values at each point of interest (mslp1Arctic)
I wrote the following code that seems to be working but looks too long/complicated for what I am actually trying to do...
%% Load %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load('data.mat')
%variables:
% -updownLat
% -updownLon
% -latMat
% -lonMat
% -mslp1
%% Main %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%convert matrix to vector
LatVec = latMat(:); %vector form of the latitude matrix
LonVec = lonMat(:); %vector form of the longitude matrix
%index of points of interest
for k = [1:length(updownLat)]
matchingLat = find(LatVec == updownLat(k)); %find index of all matching lat for point of interest
matchingLon = find(LonVec == updownLon(k)); %find index of all matching lon for point of interest
index(k) = intersect(matchingLat,matchingLon); %find the index of the single matching point
end
%transpose
indexArctic = transpose(index);
%make new matrices
empty = nan(1440,121); %create matrix of right size with only NaNs
%1)
latMatMask = empty;
latMatMask(indexArctic) = updownLat;
%2)
lonMatMask = empty;
lonMatMask(indexArctic) = updownLon;
%3)
mslp1Mask = empty;
mslp1Mask(indexArctic) = mslp1(indexArctic);
%4)
mslp1Arctic = mslp1(indexArctic);
Thank you! :)

Accepted Answer

Voss
Voss on 13 Jan 2022
Edited: Voss on 13 Jan 2022
S = load('data.mat');
[~,ridx] = ismember(S.updownLon,S.lonMat(:,1));
[~,cidx] = ismember(S.updownLat,S.latMat(1,:));
[n,m] = size(S.lonMat);
idx = sub2ind([n m],ridx,cidx);
latMatMask = NaN(n,m);
latMatMask(idx) = S.updownLat;
lonMatMask = NaN(n,m);
lonMatMask(idx) = S.updownLon;
mslp1Mask = NaN(n,m);
mslp1Mask(idx) = S.mslp1(idx);
mslp1Arctic = S.mslp1(idx);
figure();
imshow(~isnan(mslp1Mask));

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by