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.
Deleting/selecting and subsetting specific slices in a multi-dimensional array
6 views (last 30 days)
Show older comments
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?
Accepted Answer
Sindar
on 31 May 2020
Edited: Sindar
on 31 May 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 Comments
Sindar
on 31 May 2020
Edited: Sindar
on 31 May 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
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!