2D indexing of 3D array

1 view (last 30 days)
NA
NA on 16 Oct 2014
Answered: NA on 26 Oct 2014
I'm wondering if there is a better way to achieve the following without the for loop:
A=rand(20,5);
index=[1 2 3; 10 11 12; 15 16 17]';
output=zeros(size(index,1),size(index,2),size(A,2));
for I=1:size(A,2),
temp=A(:,I);
output(:,:,I)=temp(index);
end
Thank you

Accepted Answer

the cyclist
the cyclist on 16 Oct 2014
% Your input data
A=rand(20,5);
index=[1 2 3; 10 11 12; 15 16 17]';
% Get the dimensions, for convenience and clarity
a2 = size(A,2);
[i1 i2] = size(index);
% Calculate the output
output = reshape(A(index,1:a2),[i1 i2 a2])

More Answers (1)

NA
NA on 26 Oct 2014
Thank you for all the contributions. This was very educational and helpful!

Community Treasure Hunt

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

Start Hunting!