[英]flip two values in rows of 2d numpy array
我編寫了以下代碼來翻轉二維 numpy 數組行中的兩個值。 但是,我想知道是否有更好的方法來做到這一點。 提前致謝
def mtn(offg):
for row in range(offg.shape[0]):
point1 = random.randint(0, 139)
point2 = random.randint(0, 139)
temp = offg[row, point1]
offg[row, point1] = offg[row, point2]
offg[row, point2] = temp
return offg
您可以使用高級索引:
offg[:,[p1, p2]] = offg[:,[p2, p1]]
如果每一行的點都相同,你可以使用類似的代碼
tmp = offg[:,p1].copy()
offg[:,p1] = offg[:,p2]
offg[:,p2] = tmp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.