Python | Numpy matrix.diagonal ()

Using the Numpy matrix.diagonal () method, we can find the diagonal element from the given matrix and output the result as a one-dimensional matrix.

Syntax: matrix.diagonal()

Return: Return diagonal element of a matrix

Example # 1:
In this example, we can see that using the matrix. diagonal () we can find elements on the diagonal of the matrix.

# import important module into python

import numpy as np

 
# make a matrix with NumPy

gfg = np.matrix ( `[6, 2; 3, 4] ` )

  
# applying the matrix.diagonal () method

geeks = gfg.diagonal ()

 

print (geeks)

Output:

 [[6 4]] 

Example # 2:

# import important module in python

import numpy as np

 
# make a matrix with NumPy

gfg = np.matrix ( ` [1, 2, 3; 4, 5, 6; 7, 8, 9] ` )

  
# using the matrix.diagonal () method

geeks = gfg.diagonal ()

 

print (geeks)

Exit:

 [[1 5 9]]