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

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 个评论

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.

请先登录,再进行评论。

 采纳的回答

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 个评论

Hello, thanks for the reply. I am getting the following error:
Error using true - Size inputs must be scalar.
whoops, I changed my earlier version incorrectly. Should work now
Hello, thank you! This does work :) Would you mind elaborating on the logic a little bit - the purpose of the and and the true?
What you have is a 699x699 logical matrix. My understanding is that you want to index the associated elements from every page (3rd dimension) of your matrix. So, you need a 699x699x60 logical matrix. If we do elementwise multiplication of a 699x699 matrix and a 1x1x60 matrix, we get a 699x699x60 matrix. Now, since we are specifically working with logical elements, it would be good if we could stay logical throughout to take advantage of the efficiency. So, our 1x1x60 matrix should be logical:
true(1,1,60)
I use size() to ensure a little more flexibility if the original matrix changes size.
Likewise, we would like the elementwise multiplication to stay in boolean space, so we use and(). (since we are 'and'-ing a true, subset_3D will be 'true' in exactly the same rows/columns as subset_idx is true)
This should result in a 699x699x60 logical matrix where each page is the same as subset_idx
Note that this should definitely work for subsetting, but deletion is more complicated. Consider a simple 2D case:
>> x = [1 2;3 4]
x =
1 2
3 4
Now, if you want to remove the '3', what do you get?
On paper, we could naively leave a blank there:
1 2
4
but that doesn't make sense either mathematically or to Matlab:
>> x(2,1)=[]
A null assignment can have only one non-colon index.
Matlab would have us use NaN:
>> x(2,1)=NaN
x =
1 2
NaN 4
and that may be what you want.
But, there's another option: we work on the subset directly:
>> idx=[true true; false true]
idx =
2×2 logical array
1 1
0 1
>> x(idx)
ans =
1
2
4
>> x(idx) = x(idx).^2
x =
1 4
3 16
since the subset is treated as a vector, it doesn't matter how many dimensions the original matrix had.
But, you have to be careful that you consistently work with corresponding subsets:
>> x = [1 2;3 4]
x =
1 2
3 4
>> y = [1 1;2 3]
y =
1 1
2 3
>> x(idx) = x(idx) + y
Matrix dimensions must agree.
>> x(idx) = x(idx) + y(idx)
x =
2 3
3 7
Excellent, just one clarification: is this 3D reconstruction the same/better than taking this 2D logical map and simply conforming it to the third dimension? I can conform it using conform_dims for example. That would simply repmat the logic map in 2D to the third dimension. Is this option the same/better than conform?
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 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by