Is there a way that indexing a matrix of data with a matrix of indices?
![](https://secure.gravatar.com/avatar/45c62239fe401b600e94f7ec05e598a9.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/b4f6d4f8b501cb05fd054944a166a121.jpg?s=120&d=mm&r=g)
On Wed, 2017-11-29 at 14:56 +0000, ZHUO QL (KDr2) wrote:
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
![](https://secure.gravatar.com/avatar/b4f6d4f8b501cb05fd054944a166a121.jpg?s=120&d=mm&r=g)
On Wed, 2017-11-29 at 14:56 +0000, ZHUO QL (KDr2) wrote:
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
participants (2)
-
Sebastian Berg
-
ZHUO QL (KDr2)