我正在使用两个a.shape =(n,m,l)和b.shape =(n,m)的numpy数组a和b,我想将它们相乘以获得c.shape = (n,m,l)等同于
for ei in range(a.shape[0]):
for ej in range(a.shape[1]):
c[ei, ej, :] = b[ei, ej] * a[ei, ej, :]
,但语法类似于np.multiply,用于将两个具有相同形状的数组相乘。这可能吗?
答案 0 :(得分:0)
两者之一(根据评论中的@PaulBroderson)
c = a * b[:, :, None]
或使用np.einsum
:
c = np.einsum('ij, ijk -> ijk', a, b)