2017-06-22 40 views

回答

6

使用numpy,这是一种非常流行的Python线性代数库

>>> import numpy as np 
>>> a = np.matrix([[1,1], [1,1]]) 
>>> b = np.matrix([[1,1], [1,1]]) 
>>> a + b 
matrix([[2, 2], 
     [2, 2]]) 
+0

你可能打算使用'np.array' –

+0

@NilsWerner一个数组也可以在这里工作,但我的意思是我写了:) – CoryKramer

+0

我仍然建议改变它,因为[SciPy本身建议使用'array' '矩阵',如果你没有特别需要'矩阵'中的特征](https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html#array-or - 矩阵 - 这,应该-I-使用)。 –

1

可以使用列表理解

a = [[1,1], [1,1]] 
b = [[1,1], [1,1]] 
c = [[a[i][j] + b[i][j] for j in range(len(a[0])) ] for i in range(len(a))]