Indexing a 4-D array using a logical matrix
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!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! 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! 4-d, indexing MATLAB Answers — New Questions









