矩阵数学的匹配矩阵大小

问题描述 投票:0回答:1

我正在尝试使用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)
python numpy matrix matrix-multiplication
1个回答
0
投票

请尝试以下可能有帮助的方法,

x + np.atleast_3d(np.identity(2))

其中x是您的(2,2,401)数组。上面的代码应该直接在整个数组上工作,而无需使用循环。

© www.soinside.com 2019 - 2024. All rights reserved.