Increase numpy array elements using array as index
To correctly handle the duplicate indices, you'll need to use np.add.at
instead of +=
. Therefore to update the first row of A
, the simplest way would probably be to do the following:
>>> np.add.at(A[0], [1,1,1,2], 1)
>>> A
array([[0, 4, 3, 3, 4],
[5, 6, 7, 8, 9]])
The documents for the ufunc.at
method can be found here.