[Numpy-discussion] Faster than ndindex?

Eleftherios Garyfallidis garyfallidis at gmail.com
Sat Dec 4 15:05:02 EST 2010


This is beautiful! Thank you Pauli.

On Sat, Dec 4, 2010 at 7:16 PM, Pauli Virtanen <pav at iki.fi> wrote:

> On Sat, 04 Dec 2010 19:00:43 +0000, Eleftherios Garyfallidis wrote:
> [clip]
> > I am using ndindex and then a for loop. Is there a better/faster way?
>
> Yes:
>
> import numpy as np
> from numpy import newaxis
>
> x = np.zeros((200, 200, 200, 3))
> x[...,0] = np.arange(200)[:,newaxis,newaxis]
> x[...,1] = np.arange(200)[newaxis,:,newaxis]
> x[...,2] = np.arange(200)[newaxis,newaxis,:]
> x[1,3,2]
> # -> array([ 1.,  3.,  2.])
>
> Depending on what you use this array for, it's possible that you can
> avoid constructing it (and use broadcasting etc. instead).
>
> --
> Pauli Virtanen
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20101204/52a460c8/attachment.html>


More information about the NumPy-Discussion mailing list