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

vectorized matrix list application in numpy

the problem i am trying to solve is as follows. I am given a matrix of arbitrary dimension representing indices of a list, and then a list. I would like to get back a matrix with the list elements swapped for the indices. I can’t figure out how to do that in a vectorized way:
i.e if z = [[0,1], [1,0]] and list = [20,10], i’d want [[20,10], [10,20]] returned.

>Solution :

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

When they both are np.array, you can do indexing in a natural way:

import numpy as np

z = np.array([[0, 1], [1, 0]])
a = np.array([20, 10])

output = a[z]
print(output)
# [[20 10]
#  [10 20]]
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