【发布时间】:2019-04-12 11:50:33
【问题描述】:
我想通过简单地沿第一个轴执行相同的加法来添加两个不同维度的数组。
非矢量化解决方案:
x = np.array([[[1,2],[3,4],[5,6]],[[7,8],[9,0],[1,2]],[[3,4],[5,6],[7,8]],[[9,0],[1,2],[3,4]]]) #shape (4,3,2)
y = np.array([[1,2],[3,4],[5,6]]) #shape (3,2)
ans = np.empty(x.shape)
for i in range(x.shape[0]):
ans[i] = x[i] + y
print(ans) #shape (4,3,2)
我怎样才能适当地进行这个广播?
【问题讨论】: