当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python numpy.tri()用法及代码示例


numpy.tri(R,C = None,k = 0,dtype ='float'):创建一个数组,数组在给定的对角线(约k)处和对角线以下(小于k),在其他位置为0。
参数:

R     : Number of rows
C     : [optional] Number of columns; By default R = C
k     : [int, optional, 0 by default]
               Diagonal we require; k>0 means diagonal above main diagonal or vice versa.
dtype : [optional, float(byDefault)] Data type of returned array.  
# 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.]]  



相关用法


注:本文由纯净天空筛选整理自 numpy.tri() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。