1

I have the following script:

values = np.array([1, 1, 1, 1, 1, 1]) # ndarray with some values, of length n = 6
target = np.array([1, 1, 0, 2, 3, 2], dtype = int) # indices, of length n = 6
sums = np.zeros(4) # target to sum up
sums[target] += values

I think it's pretty clear what should happen: The values in value should be accumulated in sums at the index corresponding to target.

Unfortunately, it does not work as expected, no surprise though.

Is there an efficient way to implement it in Numpy?

Michael
  • 7,407
  • 8
  • 41
  • 84

0 Answers0