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

조회 수: 6 (최근 30일)
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
Sai Prasanth
Sai Prasanth 2020년 5월 29일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Sindar
Sindar 2020년 5월 31일
편집: Sindar 2020년 5월 31일
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
Sindar
Sindar 2020년 5월 31일
편집: Sindar 2020년 5월 31일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by