簡體   English   中英

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

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

我有一個名為coords的2d坐標的N×2 numpy數組,另一個名為plane 2d numpy數組。 我想做的就像

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

但沒有for循環來提高效率。 如何使用向量化代碼執行此操作? numpy中要使用哪個函數或方法?

您可以嘗試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]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM