[SciPy-user] 3 dimensional arrays

Steve Schmerler elcorto@gmx....
Thu Aug 23 09:59:53 CDT 2007


Perez Kawumi wrote:
> Hi ,
> I'm trying to create arrays in three-d. And then break them down into planes(e.g. h(:,:,1) below). I have attached a piece of matlab code and how it runs. I have also attached what I have done in python but having problems verifying if it's doing exactly what I want. Just wondering if anyone can help me do this in python. I 
> Thanks Perez
> 
> Matlab code
>>> b= rand(3,3,2)*10
> b(:,:,1) =
>     5.3960    6.7735    3.1040
>     6.2339    8.7683    7.7908
>     6.8589    0.1289    3.0730
> 
> b(:,:,2) =
>     9.2668    0.7067    5.1625
>     6.7872    0.1193    4.5820
>     0.7432    2.2715    7.0320
> 
> Python code
>>>> b = random.random((3,3,2))*10
>>>> b
> array([[[ 2.90701573,  5.43543163],
>         [ 3.23435142,  6.3642547 ],
>         [ 4.63504825,  4.4083266 ]],
>        [[ 6.21107005,  8.40185679],
>         [ 1.25017666,  6.25796949],
>         [ 8.69343579,  0.48324745]],
>        [[ 3.19007467,  2.65135416],
>         [ 5.8586314 ,  6.72327567],
>         [ 8.14780457,  2.39946201]]])
>>>> m = b[:,:,1]
>>>> m
> array([[ 5.43543163,  6.3642547 ,  4.4083266 ],
>        [ 8.40185679,  6.25796949,  0.48324745],
>        [ 2.65135416,  6.72327567,  2.39946201]])
> 

Have you checked these?
http://www.scipy.org/Tentative_NumPy_Tutorial
http://www.scipy.org/NumPy_for_Matlab_Users

I think you want to do this:

In [14]: a = randn(2,3,3)*10

In [15]: a
Out[15]:
array([[[ -9.2518157 ,  -4.64420333, -16.10449661],
         [ -0.29522799,   0.55422247, -17.0803633 ],
         [  3.36247204,  11.06595503,  -1.54147594]],

        [[ -1.20231878,   7.46063533,  -5.28938933],
         [  0.60121811,  -8.04018663,   0.74826369],
         [  6.78279031,   2.4766683 ,  10.34622062]]])

In [16]: a[0,:,:]
Out[16]:
array([[ -9.2518157 ,  -4.64420333, -16.10449661],
        [ -0.29522799,   0.55422247, -17.0803633 ],
        [  3.36247204,  11.06595503,  -1.54147594]])

In [17]: a[0,::]
Out[17]:
array([[ -9.2518157 ,  -4.64420333, -16.10449661],
        [ -0.29522799,   0.55422247, -17.0803633 ],
        [  3.36247204,  11.06595503,  -1.54147594]])

In [18]: a[1,::]
Out[18]:
array([[ -1.20231878,   7.46063533,  -5.28938933],
        [  0.60121811,  -8.04018663,   0.74826369],
        [  6.78279031,   2.4766683 ,  10.34622062]])

In [24]: a.shape
Out[24]: (2, 3, 3)

So here you have a 3d-array which consists of two 2d arrays of shape 3x3 
("planes" if you like).

-- 
cheers,
steve

Random number generation is the art of producing pure gibberish as quickly as 
possible.


More information about the SciPy-user mailing list