在Numpy数组中累加常数 [英] Accumulate constant value in Numpy Array
本文介绍了在Numpy数组中累加常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对numpy数组的某些特定单元格进行+1运算,但是如果没有慢速循环,我将找不到任何方法:
I'm trying to sum +1 to some specific cells of a numpy array but I can't find any way without slow loops:
coords = np.array([[1,2],[1,2],[1,2],[0,0]])
X = np.zeros((3,3))
for i,j in coords:
X[i,j] +=1
结果:
X = [[ 1. 0. 0.]
[ 0. 0. 3.]
[ 0. 0. 0.]]
X[coords[:,0],coords[:,1] += 1
返回
X = [[ 1. 0. 0.]
[ 0. 0. 1.]
[ 0. 0. 0.]]
有帮助吗?