Extracting elements of a matrix based on latitude & longitude limits
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
Alex Hanes
2022년 10월 24일
편집: Alex Hanes
2022년 10월 25일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Cartesian Coordinate System Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!