## Column-wise inexing of matrix

### Ivan Gonzalez Bustos (view profile)

on 9 Apr 2019
Latest activity Edited by Guillaume

on 10 Apr 2019

### James Tursa (view profile)

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.

R2016a

### James Tursa (view profile)

on 9 Apr 2019
Edited by James Tursa

### James Tursa (view profile)

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.

Guillaume

### Guillaume (view profile)

on 9 Apr 2019
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.
James Tursa

### James Tursa (view profile)

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)))
Guillaume

on 10 Apr 2019
Indeed!