[Numpy-discussion] array manupulation

Aronne Merrelli aronne.merrelli at gmail.com
Sun May 26 04:43:56 EDT 2013


On Sun, May 26, 2013 at 4:30 AM, Sudheer Joseph <sudheer.joseph at yahoo.com>wrote:

> Dear Brian,
>                 I even tried below but no luck!
> In [138]: xx=np.zeros(11)
> In [139]: xx
> Out[139]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>
> In [147]: xx.shape
> Out[147]: (11,)
> In [140]: xx=np.array(xx)[np.newaxis]
> In [141]: xx.shape
> Out[141]: (1, 11)
> In [142]: xx=xx.T
> In [143]: xx.shape
> Out[143]: (11, 1)
> In [144]: csum.shape
> Out[144]: (11, 5)
> In [145]: np.vstack((xx,csum))
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call last)
> /media/SJOITB/SST_VAL/<ipython-input-145-2a0a60f68737> in <module>()
> ----> 1 np.vstack((xx,csum))
>
> /usr/local/lib/python2.7/dist-packages/numpy-1.7.0-py2.7-linux-x86_64.egg/numpy/core/shape_base.pyc
> in vstack(tup)
>     224
>     225     """
> --> 226     return _nx.concatenate(map(atleast_2d,tup),0)
>     227
>     228 def hstack(tup):
>
> ValueError: all the input array dimensions except for the concatenation
> axis must match exactly
>
>
>

You've transposed the arrays, so now you need to stack the other way. So,
you need to use hstack to concatenate arrays with the same column length
(first axis), or vstack to concatenate arrays with the same row length
(second axis). For example:

In [110]: xx1 = np.zeros((1,7)); cc1 = np.ones((3,7))

In [111]: xx2 = np.zeros((7,1)); cc2 = np.ones((7,3))

In [112]: np.vstack((xx1, cc1))
Out[112]:
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.]])

In [113]: np.hstack((xx2, cc2))
Out[113]:
array([[ 0.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.]])


Also, I would highly recommend studying the NumPy for MATLAB users guide:

http://www.scipy.org/NumPy_for_Matlab_Users

These issues (any many more) are discussed there.


Cheers,
Aronne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130526/26158775/attachment.html>


More information about the NumPy-Discussion mailing list