0

I wrote the following code to flip two values in rows of 2d numpy array. However, I am wondering if there is a better way to do this. thanks in advance

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
0

2 Answers 2

1

You could use advanced indexing:

offg[:,[p1, p2]] = offg[:,[p2, p1]]
Sign up to request clarification or add additional context in comments.

Comments

0

if points are same in each row, u can use code like

tmp = offg[:,p1].copy()
offg[:,p1] = offg[:,p2]
offg[:,p2] = tmp

3 Comments

Thank you, unfortunately they are not the same
So I don't see some problems in your way
yes but I was looking for more advanced way to do it like the advanced indexing indicated by tstanisl above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.