Python numpy.tri()
numpy.tri()(R, C =None,k = 0, dtype = ‘ float ‘):创建一个数组,在给定对角线处和对角线以下的值为1(约为k),其他地方的值为0。
参数 :
R : 行的数量
C : [可选] 列的数量;默认情况下,R = C
k : [int, optional, 0 byDefault] 我们需要的对角线;k>0意味着对角线高于主对角线,反之亦然。
dtype : [optional, float(byDefault)] 返回阵列的数据类型。
示例:
# Python Program illustrating
# numpy.tri method
import numpy as geek
print("tri with k = 1 : \n",geek.tri(2, 3, 1, dtype = float), "\n")
print("tri with main diagonal : \n",geek.tri(3, 5, 0), "\n")
print("tri with k = -1 : \n",geek.tri(3, 5, -1), "\n")
输出 :
tri with k = 1 :
[[ 1. 1. 0.]
[ 1. 1. 1.]]
tri with main diagonal :
[[ 1. 0. 0. 0. 0.]
[ 1. 1. 0. 0. 0.]
[ 1. 1. 1. 0. 0.]]
tri with k = -1 :
[[ 0. 0. 0. 0. 0.]
[ 1. 0. 0. 0. 0.]
[ 1. 1. 0. 0. 0.]]