[Numpy-discussion] np.dstack vs np.concatenate?

Chao YUE chaoyuejoy at gmail.com
Mon Dec 10 17:30:05 EST 2012

Dear all,

I want to concate 13 mXn arrays into mXnX13 array.  np version 1.6.2

I know the correct way to do that like here:

yet the np.dstack documentation also gives something like this:
Equivalent to ``np.concatenate(tup, axis=2)``.

so I tried this:

In [10]: a = np.arange(8).reshape(2,4)

In [11]: b=np.arange(9,17).reshape(2,4)

In [12]: abd = np.dstack((a,b))

In [13]: abd.shape
Out[13]: (2, 4, 2)

In [14]: np.testing.assert_array_equal(abd[...,0],a)

In [15]: np.testing.assert_array_equal(abd[...,1],b)

In [16]: np.concatenate((a,b),axis=2)
ValueError                                Traceback (most recent call last)
<ipython-input-16-ed1e1b2f1436> in <module>()
----> 1 np.concatenate((a,b),axis=2)

ValueError: bad axis1 argument to swapaxes

so I guess for the 13 arrays, np.dstack will work. I can also do something

array_list_old = [arr1, arr2, arr3, arr4, arr5, arr6]

array_list = [arr[...,np.newaxis] for arr in array_list_old]
array = np.concatenate(tuple(array_list),axis=2)

So is there some inconsistency in the documentation?



Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121210/f997c513/attachment.html>

More information about the NumPy-Discussion mailing list