I want to change values in one array using values from another array via a third array of indices, such as:
import numpy as np
F = np.zeros((4,3)) # array I wish to change
f = np.array([[3,4,0],[0,0,1]]) # values I wish to add to F
i = np.array([2, 2]) # indices in F I wish to affect
Lets use this data to do a += operation on F on each index i using the values in f
for id in xrange(len(i)):
F[i[id]] += f[id]
# F[2] is now equal to np.array([ 3., 4., 1.]) because
# both values in f have been correctly added to F[2]
I assumed I could do the same operation in one line like so:
F[i] += f
# F[2] is now equal to np.array([ 0., 0., 1.])
# i expected np.array([ 3., 4., 1.])
But this fails. The result I expected was np.array([ 3., 4., 1.])
If i had been a list of different indices (ex: array([0, 2])) then F[0] and F[2] would have been set to the proper items in f, but in this case I want to do a += operation, and when indices repeat I want the result to be cumulative.
Isn't there a way to do this in a simple one line operation?