InquilineKea
InquilineKea

Reputation: 891

How do I do 2D logical array indexing in a 3D matrix?

TEMP = taux(LAT < 0 & LAT > -9 & LON < 90 & LON > 70,timeIndex);

outputs this wonderful error:

Index exceeds matrix dimensions.

taux is 360x160x192.

LAT is 160x360, as is LON. timeIndex is 192x1 (it's an index of values for which time is between 1998 and 1999, for example)

Even if I invert the matrix or try the following, the error still exists.

TEMP = taux((LAT < 0 & LAT > -9 & LON < 90 & LON > 70)',timeIndex)

OR

TEMP = taux(LAT < 0 & LAT > -9 & LON < 90 & LON > 70,LAT < 0 & LAT > -9 & LON < 90 & LON > 70,timeIndex);

Upvotes: 0

Views: 524

Answers (1)

Bjoern H
Bjoern H

Reputation: 600

As far as I know you can only use indexing to select parts of each dimension individually or use a single indexing matrix for all dimensions in one go. There's no way to join some dimensions together and leave some on their own.

I'm gonna guess that you want values from a specific latitude and longitude to stay grouped in some way. One approach is then to find the linear indices of the values to select based on the LAT and LON matrices and then offset these for each time index:

latlon = (LAT < 0 & LAT > -9 & LON < 90 & LON > 70)';
timeOffset = size(taux, 1) * size(taux, 2) * (timeIndex' - 1)
indices = bsxfun(@plus, find(latlon), timeOffset);
TEMP = taux(indices);

This will return a matrix where each row corresponds to a different latitude and longitude and each column to a different time.

Upvotes: 1

Related Questions