How to create matrix with only 0 and fill the center diagonal with 1 using numpy in Python

1 Answer

0 votes
import numpy as np

matrix = np.zeros((4, 4), int) 

np.fill_diagonal(matrix, 1)

print(matrix)



'''
run:

[[1 0 0 0]
 [0 1 0 0]
 [0 0 1 0]
 [0 0 0 1]]

'''

 



answered Nov 17, 2022 by avibootz
...