两列矩阵的总和

use*_*374 0 python numpy sum

我有两个矩阵:

mx1 = np.matrix([[2,9,9],[2,5,8],[7,2,9]])

[[2 9 9]
 [2 5 8]
 [7 2 9]]

mx2 = np.matrix([[7,1,3],[5,8,2],[6,9,5]])

[[7 1 3]
 [5 8 2]
 [6 9 5]]
Run Code Online (Sandbox Code Playgroud)

我想逐行做矩阵产品,但总和.

即,得到的矩阵元素[1,1]应计算为:

(2 + 7)+(9 + 5)+(9 + 6)= 38

元素[1,2]:

(2 + 1)+(9 + 8)+(9 + 9)= 38

等等.

一些聪明的方法吗?

Mr.*_*. T 5

如何使用numpy广播?

mx1 = np.matrix([[2,9,9],[2,5,8],[7,2,9]])
mx2 = np.matrix([[7,1,3],[5,8,2],[6,9,5]])
res = np.sum(mx1, axis = 1) + np.sum(mx2, axis = 0)
Run Code Online (Sandbox Code Playgroud)