Is there a way that indexing a matrix of data with a matrix of indices?

Hi, all suppose: - D, is the data matrix, its shape is M x N- I, is the indices matrix, its shape is M x K, K<=N Is there a efficient way to get a Matrix R with the same shape of I so that R[x,y] = D[x, I[x,y]] ? A nested for-loop or list-comprehension is too slow for me. Thanks. ---- ZHUO QL (KDr2) http://kdr2.com

On Wed, 2017-11-29 at 14:56 +0000, ZHUO QL (KDr2) wrote:
Hi, all
suppose:
- D, is the data matrix, its shape is M x N
- I, is the indices matrix, its shape is M x K, K<=N
Is there a efficient way to get a Matrix R with the same shape of I so that R[x,y] = D[x, I[x,y]] ?
A nested for-loop or list-comprehension is too slow for me.
Advanced indexing can do any odd thing you might want to do. I would not suggest to use the matrix class, but always use the array class in case you are doing that though.
This should do the trick, I will refer the the documentation for how it works, except that it is basically:
R[x,y] = D[I1[x, y], I2[x, y]]
R = D[np.arange(I.shape[0])[:, np.newaxis], I]
- Sebastian
Thanks.
ZHUO QL (KDr2) http://kdr2.com _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
participants (2)
-
Sebastian Berg
-
ZHUO QL (KDr2)