[Numpy-discussion] Stacking a 2d array onto a 3d array

Dewald Pieterse dewald.pieterse@gmail....
Tue Oct 26 19:15:19 CDT 2010


Starting with:

In [93]: test =
> numpy.array([[[1,1,1],[1,1,1]],[[2,2,2],[2,2,2]],[[3,3,3],[3,3,3]]])
>
> In [94]: test
> Out[94]:
> array([[[1, 1, 1],
>         [1, 1, 1]],
>
>        [[2, 2, 2],
>         [2, 2, 2]],
>
>        [[3, 3, 3],
>         [3, 3, 3]]])
>
> Slicing the complete first row:
>
> In [95]: firstrow = test[0,:,:]
>
> In [96]: firstrow
> Out[96]:
> array([[1, 1, 1],
>        [1, 1, 1]])
>

I want to stack firstrow onto test to end up with:

([[[1, 1, 1],
>         [1, 1, 1]],
>
>        [[1, 1, 1],
>         [1, 1, 1]],
>
>        [[2, 2, 2],
>         [2, 2, 2]],
>
>        [[3, 3, 3],
>         [3, 3, 3]]]
>


vstack wants the array dimensions to be the same, is this possible without
doing 1 dimensional reshape, the actual data I want to do this on is some
what larger.

 numpy.vstack((firstrow,test))
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call last)
>
> /mnt/home/home/bmeagle/M/programme/analiseerverwerkteprent.py in <module>()
> ----> 1
>       2
>       3
>       4
>       5
>
> /usr/lib64/python2.6/site-packages/numpy/core/shape_base.py in vstack(tup)
>     212
>     213     """
> --> 214     return _nx.concatenate(map(atleast_2d,tup),0)
>     215
>     216 def hstack(tup):
>
> ValueError: arrays must have same number of dimensions
>


What is the correct python way to do this?


-- 
Dewald Pieterse
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20101027/53c7b60b/attachment-0001.html 


More information about the NumPy-Discussion mailing list