Indexing a 4-D array using a logical matrix

30 次查看(过去 30 天)
Hello, I have a few 4-D arrays, originally netCDF files, which represent mapping data. Each 4-D array, or map, is lat x lon x percentile x frequency. I have the actual lat, lon, percentile, and frequency values sorted as seperate vectors.
Each map is actually 1144 x 1051 x 7 x 3, so for practice:
map=rand(1144,1051,7,3)
I have a polygon I want to subset the data by, and I have created an index by:
eez=shaperead('./ShapeFiles/eez/eez.shp');
lat = ncread(ncfile, '/lat');
lon = ncread(ncfile, '/lon');
[LonGrid, LatGrid] = meshgrid(lon, lat); %make a grid with the latlongs
Y=eez.Y(~isnan(eez.Y))'; %removing NAs and transposing the array
X=eez.X(~isnan(eez.X))';
inPoly = inpolygon(LonGrid, LatGrid,X,Y);
How do I use the inPoly logical to subset the 4-D map, just for the lat/lon, but apply it equally to all other dimensions (percentile/frequency), so I have a resulting 4-D array with the subsetted data? If I just index it by:
test=map(inPoly);
It returns an array that I am unsure how to apply to the original 4-D matrix. Ideally, it would return a 4D matrix with NaNs outside of the polygon.
Thanks in advance!
  2 个评论
Matt J
Matt J 2025-10-13,14:52
编辑:Matt J 2025-10-13,15:45
How do I use the inPoly logical to subset the 4-D map, just for the lat/lon, but apply it equally to all other dimensions (percentile/frequency), so I have a resulting 4-D array with the subsetted data
It is not clear why you would expect a 4-D array, rather than a 3-D array, as a result. When you extract the interior pixels of an arbitrary polygon, the pixels do not have any natural rectangular shape, and must be organized as a 1D list.
Emily T. Griffiths
Emily T. Griffiths 2025-10-13,16:14
Good point! I will edit the question. I would want there to be NaNs outside the polygon.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2025-10-13,14:50
编辑:Matt J 2025-10-13,15:10
Assuming you are trying to extract the polygon interiors, you could do,
[~,~,p,q]=size(map);
Map=reshape(map,[],p*q);
test=reshape( Map(inPoly,:) , p, q );
If you are only trying to mask the array so that everything outside the polygon is set to zero, then you could just do instead,
test=map.*inpPoly;
  2 个评论
Emily T. Griffiths
Emily T. Griffiths 2025-10-13,16:24
Thanks! I think ideally I would like to set everything outside the polygon to NaN. However, 0 is an inprobably number in this dataset, so this may work, as I can then reassign everything that is 0 to NaN. :)
Matt J
Matt J 2025-10-13,16:44
编辑:Matt J 2025-10-13,16:44
Thanks!
You are quite welcome, but lease Accept-click the answer if your question is resolved.
ideally I would like to set everything outside the polygon to NaN
That can be done as below:
[~,~,p,q]=size(map);
Map=reshape(map,[],p*q);
Map(~inPoly,:)=nan;
map=reshape(Map, size(map));
or possibly also,
map=map./inPoly;
map(~isfinite(map))=nan;

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Elementary Polygons 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by