
4 Dec
2010
4 Dec
'10
11:16 a.m.
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