MATLAB Answers

0

Column-wise inexing of matrix

Asked by Ivan Gonzalez Bustos on 9 Apr 2019
Latest activity Edited by 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.

  0 Comments

Sign in to comment.

Products


Release

R2016a

1 Answer

Answer by James Tursa
on 9 Apr 2019
Edited by James Tursa
on 9 Apr 2019
 Accepted Answer

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

You may want to use sub2ind instead. It does exactly the same calculations as James' code, possibly a smidge slower because it'll validate the inputs. The advantage is that it's clearer to the reader what is going on:
A(sub2ind(size(A), indx, repmat(1:size(indx, 2), size(indx, 1), 1)))
Otherwise, please write a comment after that line of code so readers understand what it does.
You have your row & column indexing reversed. It should be
A(sub2ind(size(A), indx, repmat(1:size(indx, 2), size(indx, 1), 1)))
Indeed!

Sign in to comment.