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 one with zero in identity matrix | Numpy |

I am learning Numpy. How do i get the expected output from identity matrix

identity matrix

1, 0, 0
0, 1, 0
0, 0, 1

Expected Output :

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

1, 0, 0
0, 0, 0
0, 0, 0

>Solution :

Your question is unclear, however assuming you have a matrix of zeros with 1’s on the diagonal and want to have all zeros but the first element:

a = np.array([[1,0,0],
              [0,1,0],
              [0,0,1]])
np.fill_diagonal(a, 0)
a[0,0] = 1

output:

array([[1., 0., 0.],
       [0., 0., 0.],
       [0., 0., 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