[Numpy-discussion] Faster than ndindex?
Pauli Virtanen
pav at iki.fi
Sat Dec 4 14:16:45 EST 2010
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
More information about the NumPy-Discussion
mailing list