Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belltailjp/dd5a2c17d9652e7d0717 to your computer and use it in GitHub Desktop.
Save belltailjp/dd5a2c17d9652e7d0717 to your computer and use it in GitHub Desktop.
Enforce 1st row to be smaller than the 2nd
# Construct 2-row ndarray
>>> a = numpy.random.randint(0, 100, (2, 10))
>>> a
array([[95, 31, 25, 68, 88, 25, 71, 55, 16, 14],
[57, 59, 38, 30, 78, 92, 30, 47, 89, 41]])
# Extract columns whose 1st row is larger than the 2nd.
>>> a[:, a[1] < a[0]]
array([[95, 68, 88, 71, 55],
[57, 30, 78, 30, 47]])
# Swap 1st and 2nd row of such columns
>>> a[:, a[1] < a[0]] = a[:, a[1] < a[0]][::-1]
>>> a
array([[57, 31, 25, 30, 78, 25, 30, 47, 16, 14],
[95, 59, 38, 68, 88, 92, 71, 55, 89, 41]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment