Column-wise inexing of matrix

3 views (last 30 days)
Ivan Gonzalez Bustos
Ivan Gonzalez Bustos on 9 Apr 2019
Edited: Guillaume on 10 Apr 2019
Consider the following
A = magic(4);
indx = [1 1 3 2;4 2 1 1; 1 3 2 1];
A(indx)
Because of linear indexing the code above returns:
ans =
16 16 9 5
4 5 16 16
16 9 5 16
But I would like it to return the equivalent of
[A(indx(:,1),1) A(indx(:,2),2) A(indx(:,3),3) A(indx(:,4),4)]
ans =
16 2 6 8
4 11 3 13
16 7 10 13
What is the most efficient, scalable way of doing this? Thiking of larger matrices.

Accepted Answer

James Tursa
James Tursa on 9 Apr 2019
Edited: James Tursa on 9 Apr 2019
One way that is scalable and matches your example:
A(indx + (0:size(indx,2)-1)*size(A,1))
This uses implicit expansion. On earlier versions of MATLAB you would have to use bsxfun or repmat to get the same result.
Should work as long as indx columns match the columns of A that you want extracted. If not, then you would need to modify the row indexing vector I used to match the actual column indexing you wanted.
  3 Comments
James Tursa
James Tursa on 10 Apr 2019
You have your row & column indexing reversed. It should be
A(sub2ind(size(A), indx, repmat(1:size(indx, 2), size(indx, 1), 1)))

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!