當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python numpy.diag()用法及代碼示例


numpy.diag(a,k = 0):提取並構造對角行數組
參數:

a : array_like 
k : [int, optional, 0 by default]
          Diagonal we require; k>0 means diagonal above main diagonal or vice versa.

返回值:

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 Diagnol elements : \n", geek.diag(a), "\n") 
  
print("Diagnol above main diagnol : \n", geek.diag(a, 1), "\n") 
  
print("Diagnol below main diagnol : \n", geek.diag(a, -1))

輸出:


Main Diagnol elements : 
 [  1 434  56] 

Diagnol above main diagnol : 
 [21  3] 

Diagnol below main diagnol : 
 [63 54]


相關用法


注:本文由純淨天空篩選整理自 numpy.diag() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。