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

Replace elements of numpy.ndarray specified with ndarray

I have 2D numpy.ndarray() and indices to be replaced.

idx = np.array([[1, 0],
                [2, 1],
                [2, 3],])
array = np.array([[0, 0, 0, 0],
                  [0, 0, 0, 0],
                  [0, 0, 0, 0]])

Which I want to have is like following array.

np.array([[0, 0, 0, 0],
          [1, 0, 0, 0],
          [0, 1, 0, 1]])

Elements where specified by idx in the array is replaced with an arbitrary value.

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 :

If I understand you correctly you want to index the array by 2D array idx:

array[idx[:, 0], idx[:, 1]] = 1
print(array)

Prints:

[[0 0 0 0]
 [1 0 0 0]
 [0 1 0 1]]
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