Last active
December 16, 2015 07:39
-
-
Save andrewgiessel/5400659 to your computer and use it in GitHub Desktop.
iterdim routine a generator which yields slices of an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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