Skip to content

Instantly share code, notes, and snippets.

@maxrohleder
Created November 28, 2020 14:38
Show Gist options
  • Save maxrohleder/cea98b3ead2e2fa2fd11928d676341f2 to your computer and use it in GitHub Desktop.
Save maxrohleder/cea98b3ead2e2fa2fd11928d676341f2 to your computer and use it in GitHub Desktop.
advanced indexing lets you manipulate arrays elegantly
import numpy as np
# some sample data in shaped cubic (100, 100, 100)
img = np.random.sample((100, 100, 100))
# set all values between 0.5 and 0.6 to zero
img[(img < 0.6) & (img > 0.5)] = 0
# in a small sub cube (10, 10, 10)..
# .. get index arrays of all elements above 0.6
(x, y, z) = np.where(img[:10,:10,:10] > 0.6)
# apply coordinate transforms (rotation, translation, etc)
xn, yn, zn = x+5, y+2, y+1
# copy the cube to its new position
img[xn, yn, zn] = img[x, y, z]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment