[SciPy-user] 3d arrays

Steve Schmerler elcorto@gmx....
Thu Aug 23 14:08:44 CDT 2007


Perez Kawumi wrote:
> Hi ,
> Sorry got myself very confused before I sent out that email. When i solve this equation I get  a 1*2 matrix as shown.
> 
> b(1)*a(1,:) + b(2)*a(2,:) + b(3)*a(3,:) = [25 18]

As mentioned earlier, it would be really, really nice if you'd learn some numpy
basics and terminology (especially numpy array indexing) from the docs at
scipy.org.
This would make it a lot easier to communicate you problems. Questions like
"please translate my <some_other_language>-code to numpy" is in general not a
very kind thing to do, even though many people on this list are very fluent
with Matlab which increases your chances. My own Matlab days are some time ago
but I guess I can help you out.

> 
> I want to assign all x and y coordinates in the first plane to have the value 25 and all the x and y coordinates in the second plane to the value 18. Which as you notice are the first and second elements of the eqn above.
> 
> Do you know how to do this in python? Sorry it was badly phrased ealier. Hope this makes more sense.
> Thanks Perez
> 

If I translate correctly, you have an array of shape (2,N,M), that is, two NxM
arrays "stacked behind one another", like that (here: N = M = 3):

In [75]: a = ones((2,3,3))

In [76]: a
Out[76]:
array([[[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]],

       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]]])

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

Along the 1st axis (of length 2), you have two 2d-arrays, of which each
spans across the 2nd and 3rd axis (your "x and y coordinates").
Then you want all elements of the first 3x3 array to be 25:

In [82]: a[0,...] = 25

In [83]: a
Out[83]:
array([[[ 25.,  25.,  25.],
        [ 25.,  25.,  25.],
        [ 25.,  25.,  25.]],

       [[  1.,   1.,   1.],
        [  1.,   1.,   1.],
        [  1.,   1.,   1.]]])

This is the same as a[0,:,:] = 25 and a[0,::] = 25. The 0 is an index into the
first axis, and there the first element, which is the first 3x3 array. The ...
or :,: say "take all of the remaining axes", as you know from Matlab. For the
second 3x3 array:

In [84]: a[1,...] = 18


> P.S. Can you create a 3 dimensional matrix say by just doing this
> d = random.random(3,3,2)*10
> 

You mean without the ()'s arround (3,3,2)? No. The argument you give must be a
tuple determining the shape of the desired array.

Also: Make sure that you understand that array != matrix in numpy! numpy.matrix
converts arrays into objects which behave much more like Matlab matrices: you
can have row and column vectors, which are treated as matrices (shape (1,N),
(N,1)) and much more.
For example, see
http://scipy.org/Tentative_NumPy_Tutorial#head-926a6c9b68b752eed8a330636c41829e6358b1d3
http://scipy.org/NumPy_for_Matlab_Users#head-e9a492daa18afcd86e84e07cd2824a9b1b651935
for matrix and
http://scipy.org/Numpy_Example_List#newaxis
for a way to turn a numpy 1d-array of shape (N,) into a 2d one with shape, say
(N, 1) to also emulate Matlab.

> Matlab
> 
>>> a = ([2,3;4,6;5,1])
> a =
>      2     3
>      4     6
>      5     1
>>> b = ([1,2,3])
> b =
>      1     2     3
> 
>>> c(1,1,:)= b(1)*a(1,:) + b(2)*a(2,:) + b(3)*a(3,:)
> c(:,:,1) =
>     25
> 
> c(:,:,2) =
>     18

-- 
cheers,
steve

I love deadlines. I like the whooshing sound they make as they fly by. --
Douglas Adams



More information about the SciPy-user mailing list