[SciPy-user] Create n-dim array from axis

Vincent Schut schut at sarvision.nl
Tue Mar 24 07:43:38 EDT 2009


josef.pktd at gmail.com wrote:
> On Mon, Mar 23, 2009 at 10:07 AM, Paul Hilscher
> <p.hilscher at lsw.uni-heidelberg.de> wrote:
>>> try mx, my = numpy.meshgrid(x, y)
>>> [zip(a,b) for (a,b) in zip(mx, my)]HTH,
>> Thanks a lot David, this works perfectly fine for 2-dimensions.
>> Is there any possibility to extend it to 3-dimensions with axis z, or
>> even to 4-dimensions giving axis w ?
>>
>> Thanks again,
>>
>> Paul
> 
> I would also like to know what an efficient way is to do this. For
> arbitrary dimension, I never found anything except for a brute force
> loop or recursive function.
> 
Would this do? Should work with arbitrary number of dimensions.

def create_array(startStopStep):
     # startStopStep is a tuple/list of (start,stop,step) entities to
     # define the axes
     slices = [slice(start, stop, complex(0, step)) for 
(start,stop,step) in startStopStep]
     g = numpy.mgrid[slices].transpose()
     return g

print create_array(((0,1,3), (2,3,3)))

[[[ 0.   2. ]
   [ 0.5  2. ]
   [ 1.   2. ]]

  [[ 0.   2.5]
   [ 0.5  2.5]
   [ 1.   2.5]]

  [[ 0.   3. ]
   [ 0.5  3. ]
   [ 1.   3. ]]]

Regards,
Vincent.




More information about the SciPy-User mailing list