Skip to content

Instantly share code, notes, and snippets.

@andrewgiessel
Last active December 16, 2015 07:39
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 andrewgiessel/5400659 to your computer and use it in GitHub Desktop.
Save andrewgiessel/5400659 to your computer and use it in GitHub Desktop.
iterdim routine a generator which yields slices of an array
# found here: http://stackoverflow.com/questions/1589706/iterating-over-arbitrary-dimension-of-numpy-array
import numpy as np
def iterdim(a, axis=0) :
a = np.asarray(a);
leading_indices = (slice(None),)*axis
for i in xrange(a.shape[axis]) :
yield a[leading_indices+(i,)]
# use like:
# asdf = np.random.random((100,10,3))
# for a in iterdim(asdf, axis=1):
# print a.shape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment