Index 3D Array with 2D logical array

19 次查看(过去 30 天)
I have an logical array B and an 3D array A. I wish to extract and manipulate values of A for specific k, whereby the values in the first two dimensions are given by B. I was hoping something like
A(B,[1 3 5])
would give me the values specified by B for the first, third and fifth slice of A, but MATLAB doesn't allow this kind of indexing.
In particular, I would like to switch values given by B for certain k (excuse the fauly syntax) akin to
A(B,[1 3 5]) = A(B,[2 4 6])
A minimum example of what I would like to achieve would be:
% coordinate mesh
[X,Y] = meshgrid(1:n,1:m);
% logical array B
B = (X-n/4).^2 + (Y-m/2).^2 < m;
% 3d array A
A = zeros(m,n,9);
A(:,:,1:4) = 1;
A(:,:,5:8) = 2;
A(:,:,9) = 3;
% switching of slices (in fauly syntax)
A(B,[1 2 3 ] = A(B,[5 6 7])
A(B,[5 6 7 ] = A(B,[1 2 3])
I need to do this switching of specific values in the 3. dimension for different slices of A and have not been able to come up with an efficient way of doing this. Any help would be greatly appreciated!

回答(3 个)

Stephen23
Stephen23 2020-1-25
编辑:Stephen23 2020-1-25
P = size(A,3);
F = @(b,s)repmat(b,1,1,P)&reshape(ismember(1:P,s),1,1,P); % requires >=R2016b
A(F(B,[1,2,3])) = A(F(B,[5,6,7]));
A(F(B,[5,6,7])) = A(F(B,[1,2,3]));
For MATLAB versions earlier than R2016b replace the & operation with bsxfun.
  5 个评论
Florian Radack
Florian Radack 2020-1-26
Thank you for your help so far. Unfortunately, running a basic test such as:
A = reshape(1:27,3,3,3);
B = false(3,3);
B(1,1) = true;
P = size(A,3);
F = @(b,s) bsxfun(@and, repmat(b,1,1,P), reshape(ismember(1:P,s),1,1,P));
A(F(B,[1 2 3])) = A(F(B,[3 2 1]));
yields no change in the array A.
Also, is there maybe an entirely different stratety that might be worth pursuing? The snippet of code will be placed in a (neccesary) loop to solve a time-dependent problem, and it seems calling the function F multiple times every loop might slow the code.
Walter Roberson
Walter Roberson 2020-1-26
Stephen's code, like mine, also assumed that the replacements have indices with increasing order. F(B,[1 2 3]) and F(B,[3 2 1]) are the same .
Every technique that uses logical matrices as indexing of the entire array is going to have the same problem of being insensitive to order of the panes. Logical indexing is always just "selected, or not selected" and so always works in linear indexing order, so trying to use logical indexing with [3 2 1] or other non-decreasing order is going to fail unless you take additional steps.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2020-1-25
You can do things like
mask1 = false(size(A));
mask2 = mask1;
B3 = repmat(B, [1 1 3]);
mask1(:,:,[1 2 3]) = B3;
mask2(:,:,[5 6 7]) = B3;
temp = A(mask1);
A(mask1) = A(mask2);
A(mask2) = temp;
This only works in the case where the pane indexes are increasing. It would not work if, for example, you were swapping A(B, [7 5 6])

Wolfgang
Wolfgang 2024-8-7
编辑:Wolfgang 2024-8-7
By bringing the first two dimensions with sizes to the end and the last dimension with size k to the front, you can use the logical array B as a linear index into the last two dimensions with sizes and in the first dimension with size k you can select / address whatever you want with a single indexing argument.
So the intended outcome of last two lines of the minimum example (in faulty syntax):
A(B, [1 2 3] = A(B, [5 6 7]);
A(B, [5 6 7] = A(B, [1 2 3]);
can be achieved by
% shift dimensions to form a k x m x n array
A = shiftdim(A, 2);
% use B to index into 2nd and 3rd dimensions
% swap the slices in the first dimension, but only where selected by B
A([1 2 3 5 6 7], B) = A([5 6 7 1 2 3], B);
% shift dimensions to form a m x n x k array again
A = shiftdim(A, 1);
Note: Swapping of the slices in two steps as in the minimum example will of course not work as intended.
More information on Matlab indexing can be found in this answer:

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by