Two arrays defining 2D coordinates, as array indices
A faster way is to use linear indexing directly without calling SUB2IND:
output = A( size(A,1)*(iy-1) + ix )
... think of the matrix A as a 1D array (column-wise order)
This is the one-line method which is not very efficient for large matrices
reshape(diag(A(ix(:),iy(:))),[ny nx])
A clearer and more efficient method would be to use sub2ind. I've incorporated yuk's comment for situations (like yours) when ix
and iy
have the same number of elements:
newA = A(sub2ind(size(A),ix,iy));
Also, don't confuse x
and y
for i
and j
in notation - j
and x
generally represent columns and i
and y
represent rows.