[Numpy-discussion] Faster than ndindex?

Eleftherios Garyfallidis garyfallidis@gmail....
Sat Dec 4 14:05:02 CST 2010


This is beautiful! Thank you Pauli.

On Sat, Dec 4, 2010 at 7:16 PM, Pauli Virtanen <pav@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@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20101204/52a460c8/attachment.html 


More information about the NumPy-Discussion mailing list