python中的矩陣操作

2021-08-19 06:34:02 字數 732 閱讀 7548

1 矩陣相乘

例:b1=mat([1,2])#1*2的矩陣

b2=mat([[1],[2]])#2*1的矩陣

b3=b1*b2

print(b3)

2 矩陣點乘(對應元素相乘)

例b1=mat([1,2])

b2=mat([1,2])

b3=multiply(b1,b2)

print(b3)

3 求逆矩陣

例c1=mat(eye(2,2)*0.5)#eye()函式生成對角矩陣,預設對角線為1

c2=c1.i

print(c2)

4 求轉置

例c1=mat([[1],[2]])

print(c1.t)

5 每一行每一列求和

c2=c1.sum(axis=0)

print(c2)#計算每一列的和

c3=c1.sum(axis=1)

print(c3)#計算每一行的和

c4=sum(c1[0,:])#注意這裡矩陣從0開始編碼

print(c4

6  求最大值和索引

import numpy as np

print(np.max(c1, axis=0))#輸出所有列的最大值

print(np.max(c1, axis=1))#輸出所有行的最大值

print(np.argmax(c1))#輸出最大值所在的索引

7  矩陣 陣列 列表的轉換

Python 矩陣操作

numpy庫用於矩陣運算,所以一般先導入它。from numpy import import numpy as np1 矩陣建立 建立一維陣列 a1 array 1,2,3 將陣列轉為矩陣 a1 mat a1 輸出a1 matrix 1,2,3 當然也可以對矩陣操作 a1.reshape 3,1 這...

python 逆矩陣 Python中的矩陣與逆矩陣

對於我正在做的乙個專案,我使用networkx adj matrix 函式將使用networkx建立的圖形分解為鄰接矩陣。然而,我遇到的乙個問題是,當我試圖求矩陣的逆時,分解的每乙個圖都會產生以下錯誤。在str traceback most recent call last file c eclip...

矩陣操作中的nonzero

def nonzero self nonzero indices returns a tuple of arrays row,col containing the indices of the non zero elements of the matrix.返回包含矩陣的非零元素索引的陣列 行 列 ...