Given some array (or tensor):
x = np.array([[0, 1, 0, 0, 0],
[0, 0, 0, 1, 0],
[1, 0, 0, 0, 0]])
and some indices of dimension equaling the number of rows in x:
idx = np.array([3, 1, 0]) # Index values range from (0: number of columns) in "x"!
Now if I wanted to add a certain value c to the columns of x depending on the indices idx, I would do the following:
x[range(3), idx] += c
To get:
x = np.array([[ 0, 1, 0, c, 0],
[ 0, c, 0, 1, 0],
[1+c, 0, 0, 0, 0]])
But what if I wanted to add the value c to every other column index in x, rather than the exact indices in idx?
The desired outcome (based on the above example) should be:
x = np.array([[c, 1+c, c, 0, c],
[c, 0, c, 1+c, c],
[1, c, c, c, c]])