Python| numpy matrix.diagonal()
在Numpy matrix.diagonal()
方法的帮助下,我们能够从给定的矩阵中找到一个diagonal element
,并将输出作为一维矩阵。
Syntax : matrix.diagonal()
Return : Return diagonal element of a matrix
示例 #1:
在这个例子中,我们可以看到在matrix.diagonal()
方法的帮助下,我们能够找到矩阵对角线中的元素。
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix('[6, 2; 3, 4]')
# applying matrix.diagonal() method
geeks = gfg.diagonal()
print(geeks)
输出:
[[6 4]]
示例 #2:
# import the 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]')
# applying matrix.diagonal() method
geeks = gfg.diagonal()
print(geeks)
输出:
[[1 5 9]]