Python numpy.triu()
numpy。triu(a, k = 0):返回带有三角形上半部分的数组的副本,其中包括k。
参数 :
a : 输入数组
k : [int, optional, 0 by default] 。
我们需要的对角线;k>0意味着对角线高于主对角线,反之亦然。
返回值 :
a的上三角,具有与a相同的形状和数据类型。
# Python Programming illustrating
# numpy. triu method
import numpy as geek
# string input
a = geek.matrix([[1, 21, 30],
[63 ,434, 3],
[54, 54, 56]])
print("Main Diagonal elements : \n", geek. triu(a), "\n")
print("Diagonal above main Diagonal elements : \n", geek. triu(a, 1), "\n\n")
print("Main Diagonal elements : \n", geek. triu(a, -1))
输出 :
Main Diagonal elements :
[[ 1 21 30]
[ 0 434 3]
[ 0 0 56]]
Diagonal above main Diagonal elements :
[[ 0 21 30]
[ 0 0 3]
[ 0 0 0]]
Main Diagonal elements :
[[ 1 21 30]
[ 63 434 3]
[ 0 54 56]]