Deleting/selecting and subsetting specific slices in a multi-dimensional array

8 Ansichten (letzte 30 Tage)
Hello,
I have three variables that are three-dimensional arrays of the size 699 (say, x or rows) x 699 (say, y or columns) x 60 (say, z or slices). I have first computed certain indices using certain conditions. E.g.,
idxs = Z > 5000 & W > 1 & M_tot > 0.05; % Idx is now a logical array of size 699 x 699 x 60.
Now, for further analysis, I am interested in keeping only those slices that satisfy the above conditions. In other words, I want to delete those slices where the idxs at a given x,y are zeros for all z. I used the any function below to create a subset:
subset_idx = any(idxs,3);
This creates a 699 x 699 logical array where I would like to keep all the points corresponding to 1 and delete all the rows and columns corresponding to a 0. This should give me a subset array of size nx, ny, 60 where nx and ny are much less than 699. How do I achieve this?
I tried something like this:
todelete_idx = ~(any(idxs,3)); Size : 699 x 699 logical
idxs(todelete_idx,:) = [];
However, this produces an error since the todelete_idx is a two-dimensional array and I am unable to subset it like this. What is the way around this?
  1 Kommentar
Sai Prasanth
Sai Prasanth am 29 Mai 2020
The deletion of the rows/columns is not as important as subsetting. That is, after this, I would like to perform operations on the nx, ny, 60 matrix where the x and y satisfy the condition. The intent is that I increase the computational efficiency by working on only those xs and ys that satisfy the condition.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sindar
Sindar am 31 Mai 2020
Bearbeitet: Sindar am 31 Mai 2020
This may not be the most efficient way, but you can take that 2D logical array and rebuild your 3D logical array:
subset_3D = and(subset_idx,true(1,1,size(idxs,3)));
  8 Kommentare
Sindar
Sindar am 31 Mai 2020
Bearbeitet: Sindar am 31 Mai 2020
facepalm yeah, just repeating the matrix is definitely better than my convoluted and process. Note though that while conform_dims is handy, it might lose some of the efficiency and coding directly:
subset_3D = repmat(subset_idx,[1 1 size(idxs,3)])
could be faster. It's also possible that Matlab does some magic in the background that will save you time/memory throughout, since it knows that subset_3D(:,:,15) is the same as subset_3D(:,:,1)
I'm also not sure whether conform_dims will return a logical matrix

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by