0

I want to access the elements of matrix A using T to yield a new matrix, Anew with the elements of A using Python. Is there a way to do it?

import numpy as np
A=np.array([[1,2,3],[4,5,6],[7,8,9]])
T=array([[0, 2],
       [1, 1],
       [1, 0]],dtype=int64)

Desired output:

Anew=array([[3],
            [5],
           [4]])

2 Answers 2

1

Use advanced indexing -- the 1st column of T as row index, 2nd column of T as column index:

A[T[:,0], T[:,1]]
# array([3, 5, 4])
Sign up to request clarification or add additional context in comments.

Comments

0

I'm assuming that the arrays are numpy arrays, in Your case this can be solved with:

A[T.T[0], T.T[1]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.