I have a 2D and a 3D numpy array and would like to multiply each column of the 2D array by its respective array. Eg multiplying
[[[1. 1.]
[1. 1.]
[1. 1.]]
[[1. 1.]
[1. 1.]
[1. 1.]]]
by
[[ 5 6]
[ 4 7]
[ 8 10]]
gives
[[[ 5. 5.]
[ 4. 4.]
[ 8. 8.]]
[[ 6. 6.]
[ 7. 7.]
[10. 10.]]]
My code currently is:
three_d_array = np.ones([2,3,2])
two_d_array = np.array([(5,6), (4,7), (8,10)])
list_of_arrays = []
for i in range(np.shape(two_d_array)[1]):
mult = np.einsum('ij, i -> ij', three_d_array[i], two_d_array[:,i])
list_of_arrays.append(mult)
stacked_array = np.stack(list_of_arrays, 0)
using an answer from Multiplying across in a numpy array but is there a way of doing it without a for loop? Thanks a lot, Dan