给定一些数组(或张量):
x = np.array([[0, 1, 0, 0, 0],
[0, 0, 0, 1, 0],
[1, 0, 0, 0, 0]])
以及一些维数等于x
行数的索引:
idx = np.array([3, 1, 0]) # Index values range from (0: number of columns) in "x"!
现在,如果我想根据索引idx
将某个值c
添加到x
的列中,我将执行以下操作:
x[range(3), idx] += c
得到:
x = np.array([[ 0, 1, 0, c, 0],
[ 0, c, 0, 1, 0],
[1+c, 0, 0, 0, 0]])
但是如果我想把x
rather中的值c
加到every other column index,而不是idx
中的精确指数,该怎么办?
预期结果(基于上述示例)应为:
x = np.array([[c, 1+c, c, 0, c],
[c, 0, c, 1+c, c],
[1, c, c, c, c]])