Highness
Highness

Reputation: 353

Swapping to numpy arrays element wise

I have two NumPy arrays l and g, and I want to swap the elements of l that are greater than the corresponding elements in g.

for example:

l = [0,19,1]
g = [1,17,2]

after the operation

l = [0,17,1]
g = [1,19,2]

the arrays could be multi dimensional. How do I do this efficiently in NumPy?

Upvotes: 3

Views: 96

Answers (1)

NPE
NPE

Reputation: 500953

Just use np.minimum and np.maximum:

l = np.array([0,19,1])
g = np.array([1,17,2])
l, g = np.minimum(l, g), np.maximum(l, g)

Upvotes: 3

Related Questions