【发布时间】:2020-03-18 20:20:35
【问题描述】:
我正在尝试使用 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 for 循环中显示你的意图吗?我不确定你期望得到什么结果。另外,请尽量减少示例。如果一个轴没有 400 多个元素,显示会容易得多
-
@MadPhysicist 在 python 循环中显示我的意图是什么意思?
-
目前还不清楚您期望得到什么结果。通常遇到这类问题,人们会写出实现他们期望的嵌套 for 循环,以避免任何可能的歧义。然后,我将向您展示如何对循环进行矢量化。
-
[:]什么都不做。如果要迭代 3d 数组的最后一个维度,请使用data[:, :, x]。
标签: python numpy matrix matrix-multiplication