我有一个N-by-2 numpy数组的2d坐标名为coords
,另一个2d numpy数组名为plane
。我想做的就像
for x,y in coords:
plane[x,y] = 0
但没有 for 循环以提高效率。如何使用矢量化代码? numpy中使用哪种功能或方法?
答案 0 :(得分:0)
您可以尝试plane[coords.T[0], coords.T[1]] = 0
不确定这是您想要的。例如:
让,
plane = np.random.random((5,5))
coords = np.array([ [2,3], [1,2], [1,3] ])
然后,
plane[coords.T[0], coords.T[1]] = 0
会给:
array([[ 0.41981685, 0.4584495 , 0.47734686, 0.23959934, 0.82641475],
[ 0.64888387, 0.44788871, 0. , 0. , 0.298522 ],
[ 0.22764842, 0.06700281, 0.04856316, 0. , 0.70494825],
[ 0.18404081, 0.27090759, 0.23387404, 0.02314846, 0.3712009 ],
[ 0.28215705, 0.12886813, 0.62971 , 0.9059715 , 0.74247202]])