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]])
CodePudding user response:
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])
CodePudding user response:
I'm assuming that the arrays are numpy arrays, in Your case this can be solved with:
A[T.T[0], T.T[1]]
