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

setting values in numpy matrix with array of indices

Consider a mxn matrix A. I computed the indices of the maximum of each row, yielding an array of dimension m.

How can I use this array of indices to set the values in a second matrix B of the same shape as A in each row to 0.

Example:

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

     A = [[1,2] ,[3,4]] 

     np.argmax(A,1) --> [1,1]

     B = [[1,1] ,[1,1]] 

I want to have:

  B = [[1 0] ,[1 0]] 

How can I do that?

>Solution :

you can use:

B[np.arange(B.shape[0]),np.argmax(A,1)] = 0
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