I am trying to multiply a 3D array by a 1D array, such that each 2D array along the 3rd (depth: d) dimension is calculated like:
1D_array[d]*2D_array
And I end up with an array that looks like, say:
[[
[1,1]
[1,1]]
[
[2,2]
[2,2]]
[
[3,3]
[3,3]]]
Which would be the result of correctly multiplying np.ones((3,2,2)) with [1,2,3].
I've been trying for some time now and whatever I seem to do I can't end up with this result, just variations on the theme. How do I correctly go about doing this?
Thanks for any help.
a
andb
, are you looking forb[:, None] * a
? – Damselfishb=np.ones((3,2,2))
a=np.array(range(-1,2))
ans=b[:, None]*a
But it threw an error:ValueError: operands could not be broadcast together with shapes (1,3) (3,2,2)
I'm sure I'm just doing something foolish? – Homicidea[:, None, None] * b
. – Damselfish