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

5 views (last 30 days)
A LL on 12 Jan 2022
Edited: Voss on 13 Jan 2022
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...
%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)
%2)
%3)
%4)
mslp1Arctic = mslp1(indexArctic);
Thank you! :)

Voss on 13 Jan 2022
Edited: Voss on 13 Jan 2022
[~,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);
mslp1Arctic = S.mslp1(idx);
figure(); ### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!