I would like to know how I can take index from an array and multiply with another array. I have two 4d arrays and one 2d index array:
base = np.ones((2, 3, 5, 5))
to_multiply = np.arange(120).reshape(2, 3, 4, 5)
index = np.array([[0, 2, 4, 2], [0, 3, 3, 2]])
The row index of the index array corresponds to the 1st dimension of base and to_multiply, and the value of the index array corresponds to the 3rd dimension of base. I want to take the slice from base according to the index and multiply with to_multiply.
Using for loops and np.multiply.at (because I may have same index) I can achieve it by:
for i, x in enumerate(index):
np.multiply.at(base[i, :, :, :], np.s_[:, x, :], to_multiply[i, :, :, :])
The correctness of above can be validated by:
to_multiply[0, 0, :, 0]
array([ 0, 5, 10, 15])
base[0, 0, :, 0]
array([ 0., 1., 75., 1., 10.])
However, I would like to know if there is one-line solution using np.multiply.at and np.ix_
I tried to use np.ix_ but I'm very confused about it because in this case it's multidimensional.