Python | numpy.fill_diagonal () method



Using the numpy.fill_diagonal() method, you can fill the diagonals of a numpy array with the value passed as a parameter to numpy.fill_diagonal () .

Syntax: numpy.fill_diagonal (array, value)
Return: Return the filled value in the diagonal of an array.

Example # 1:
In this example, we can see that using numpy.fill_diagonal ( ) we can get the diagonals filled with the values ​​passed as a parameter.

# NumPy import

import numpy as np

 
# using the numpy.fill_diagonal () method

array = np.array ([[ 1 , 2 ], [ 2 , 1 ]])

np .fill_diagonal (array, 5 )

  

print (array)

Output:

[[5 2]
[2 5]]

Example # 2:

# NumPy import

import numpy as np

 
# using the numpy.fill_diagonal () method

array = np.zeros (( 3 , 3 ), int )

np.fill_diagonal (array, 1 )

 

print (array)

Output:

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