[Numpy-discussion] swap elements in two arrays

Alan G Isaac aisaac@american....
Sun Feb 7 09:11:35 CST 2010

I have two 1d arrays, say `a` and `b`.
I need to swap elements if a 1d boolean criterion `to_swap` is met.

Here's one way:
	a, b = np.choose([to_swap,np.logical_not(to_swap)], [a, b])

Here is a much faster way:
         a[to_swap], b[to_swap] = b[to_swap], a[to_swap]

Other better ways?

Alan Isaac

More information about the NumPy-Discussion mailing list