Let's assume b=np.ones((3,2,2))
and a=np.array([1,2,3])
. I really do like the answer of @Alok which uses the simple a[:, None, None] * b
which surely works with your problem. What I dislike with this formulation is that it's quite dimension specific. What I mean is that it can only be used with 3 dimensional arrays, which was not true in my problem, where b could be a 1D or a 3D array with the exact same length for axis 0
. I hence found a way to accommodate it to my problem :
broad_a = np.broadcast_to(a, b.T.shape).T
result = broad_a * b
print(result)
[[
[1,1]
[1,1]]
[
[2,2]
[2,2]]
[
[3,3]
[3,3]]]
Giving also the intended result for your case.