Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extracting matrix values from indices using Python

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]])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading