Index 3D Array with 2D logical array
I have an logical array B and an 3D array A. I wish to extract and manipulate values of A for specific , 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 (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!I have an logical array B and an 3D array A. I wish to extract and manipulate values of A for specific , 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 (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! I have an logical array B and an 3D array A. I wish to extract and manipulate values of A for specific , 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 (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! 3d, matrix array, indexing, index, sub2ind MATLAB Answers — New Questions