2D indexing of 3D array
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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])
0 Comments
More Answers (1)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!