简体   繁体   English

使用2d numpy.array索引2d numpy.array

[英]index 2d numpy.array with 2d numpy.array

I have an N-by-2 numpy array of 2d coordinates named coords , and another 2d numpy array named plane . 我有一个名为coords的2d坐标的N×2 numpy数组,另一个名为plane 2d numpy数组。 What I want to do is like 我想做的就像

for x,y in coords:
    plane[x,y] = 0

but without for loop to improve efficiency. 但没有for循环来提高效率。 How to do this with vectorized code? 如何使用向量化代码执行此操作? Which function or method in numpy to use? numpy中要使用哪个函数或方法?

You can try plane[coords.T[0], coords.T[1]] = 0 Not sure this is what you want. 您可以尝试plane[coords.T[0], coords.T[1]] = 0不确定这是您想要的。 For example: 例如:

Let, 让,

plane = np.random.random((5,5))
coords = np.array([ [2,3], [1,2], [1,3] ])

Then, 然后,

plane[coords.T[0], coords.T[1]] = 0

will give: 会给:

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]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM