Suppose we have three 1-D arrays:
- A (say of length 5),
- B (the same length, 5 in the example)
- C (much longer, say of length 100)
C is initially filled with zeros. A gives indices of C elements which should be changed (they may repeat), and B gives values which should be added to initial zeros of C. For example, if A = [1, 3, 3, 3, 29] and B = [2, 3, 4, 2, 3], C[1] should become 2, C[3] - 9, C[29] - 3; all other C elements should remain 0.
I've written it as a for-loop:
for i in range(len(A) - 1):
C[A[i]] = C[A[i]] + B[i]
But is there a more efficient way to do the same in numpy in the vector form?
len(A)-1; that will skip the last element (29 & 3), asrangedoesn't include the upper bound.range(3) == [0,1,2].