I have a numpy array A and I want to modify values in it using a indexing list B. But the thing is in my slicing I can have an element of the array multiple times... This example will explain better what I mean by that :
import numpy as np
A = np.arange(5) + 0.5
B = np.array([0, 1, 0, 2, 0, 3, 0, 4])
print A[B]
returns as expected [ 0.5 1.5 0.5 2.5 0.5 3.5 0.5 4.5]
.
However if I do that :
A[B] += 1.
print A
I was expecting to obtain [ 4.5 2.5 3.5 4.5 5.5]
as the first element is repeated 4 times in the indexing vector B, but it returns [ 1.5 2.5 3.5 4.5 5.5]
.
So how can I do what I actually wanted to do? (without using any loop as I'm using that on very large arrays)