我正在尝试使用numpy执行矩阵数学。我有一个期望成为2x2x401矩阵的东西,我想使用np.add()
将其添加到一个单位矩阵中。在代码中,我尝试过:
result = []
self.data = x #where x is a 2x2x401 np array
z_sqrt = np.identity(2)
for x in range(401):
result.append(np.add(z_sqrt,self.data[:][:][x]))
#if the above gives me errors because of how I'm assigning it, I'm not there yet
我得到的错误是:
ValueError: operands could not be broadcast together with shapes (2,2) (2,401)
请尝试以下可能有帮助的方法,
x + np.atleast_3d(np.identity(2))
其中x
是您的(2,2,401)数组。上面的代码应该直接在整个数组上工作,而无需使用循环。