Extracting elements of a matrix based on latitude & longitude limits
6 views (last 30 days)
Show older comments
Dinuka Kankanige
on 22 Oct 2022
Commented: Dinuka Kankanige
on 31 Oct 2022
I'm using the following code to extract elements of matrix z (180*360) which are within the boundary enclosed by xq & yq (limits of lat & lon). It returns only a 1*4 array which is not the desired output. Could you please help in improving this. (xvertices & yvertices are based on the latitude (180*1) & longitude (360*1) matrices) -Thank you-
A=ncread('GRD-3-2002094-2002120-GRAC_JPLEM_BA01_0600_LND_v04.nc','lwe_thickness');
A=A';
z=A(:,:);
xq=[110,160,160,110];
yq=[-10,-10,-40,-40];
xvertices=[0.5,359.5,359.5,0.5];
yvertices=[-89.5,-89.5,89.5,89.5];
data=inpolygon(xq,yq,xvertices,yvertices);
data_z=z(data)
0 Comments
Accepted Answer
Alex Hanes
on 24 Oct 2022
Edited: Alex Hanes
on 25 Oct 2022
It’s not clear to me what xvertices and vertices are, but I assume your 180 data points in A correspond to the sampled points (-89.5, -88.5, …, 89.5) and likewise for the (0.5, 1.5, …, 359.5).
If this is true, then you are not using your query points correctly. You are getting a 1x4 output from inpolygon because xq and yq are both 1x4 arrays, not 180x360. To fix this, you need to use set your xq and yq to be vectors like this:
% Set query points for Z(lat,lon)
xq = transpose(0.5:1:359.5);
yq = transpose(-89.5:1:89.5);
% Generate all combinations of [xq(k),yq(k)]:
[xq,yq] = meshgrid(xq,yq);
xq = xq(:);
yq = yq(:);
% Set vertices of enclosing polygon
xvertices = [110,160,160,110];
yvertices = [-10,-10,-40,-40];
% Get points in/on polygon:
[in, on] = inpolygon(xq,yq,xvertices,yvertices);
I think you also flipped your query points with your polygon edge points.
More Answers (0)
See Also
Categories
Find more on Cartesian Coordinate System Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!