Python numpy.diag()
numpy.diag(a, k=0):提取并构造一个对角数组
参数 :
a : array_like
k : [int, optional, 0 by default] 。
我们需要的对角线;k>0意味着对角线高于主对角线,反之亦然。
返回值 :
ndarray
# Python Programming illustrating
# numpy.diag method
import numpy as geek
# matrix creation by array input
a = geek.matrix([[1, 21, 30],
[63 ,434, 3],
[54, 54, 56]])
print("Main Diagonal elements : \n", geek.diag(a), "\n")
print("Diagonal above main diagonal : \n", geek.diag(a, 1), "\n")
print("Diagonal below main diagonal : \n", geek.diag(a, -1))
输出 :
Main Diagonal elements :
[ 1 434 56]
Diagonal above main diagonal :
[21 3]
Diagonal below main diagonal :
[63 54]