[Numpy-discussion] Multidimensional neighbours

Jose Gomez-Dans jgomezdans@gmail....
Thu Aug 16 12:46:41 CDT 2012


Hi,
I've just come across Travis Oliphant's array version of the game of life
here <https://gist.github.com/3353411>. It's a really nice example of how
to efficiently find neighbours using numpy and family. However, I wanted to
apply these ideas to higher order arrays (eg GRID being three dimensional
rather than two dimensional). My previous attempts at this include the
(sorry for the ensuing horror!)
      neighbours = np.array( [ \
            # Lower row...
            x[:-2, :-2, :-2], x[:-2, :-2, 1:-1], x[:-2, :-2, 2: ],
            x[:-2, 1:-1,:-2], x[:-2, 1:-1, 1:-1], x[:-2, 1:-1, 2:],
            x[:-2, 2:,:-2], x[:-2, 2:, 1:-1], x[:-2, 2:, 2:],
            # Middle row
            x[1:-1, :-2, :-2], x[1:-1, :-2, 1:-1], x[1:-1, :-2, 2: ],
            x[1:-1, 1:-1,:-2], x[1:-1, 1:-1, 2:],
            x[1:-1, 2:,:-2], x[1:-1, 2:, 1:-1], x[1:-1, 2:, 2:],
            # Top row
            x[2:, :-2, :-2], x[2:, :-2, 1:-1], x[2:, :-2, 2: ],
            x[2:, 1:-1,:-2], x[2:, 1:-1, 1:-1], x[2:, 1:-1, 2:],
            x[2:, 2:,:-2], x[2:, 2:, 1:-1], x[2:, 2:, 2:] ] )

I think this works (it's been a while! ;D), but it's a bit messy and the
boundary condition is a bit crude. I was wondering whether there's some
other nice way of doing it, like demonstrated in the above link. Also, what
if you want to have a larger neighbourhood (say 5x5x2 instead of 3x3x3)?

While I appreciate these index tricks, I also find them quite mind boggling!
Thanks!
Jose
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120816/7c05d40b/attachment.html 


More information about the NumPy-Discussion mailing list