np.dstack vs np.concatenate?
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: http://stackoverflow.com/questions/8898471/concatenate-two-numpy-arrays-in-t... 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 like: 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? thanks, Chao -- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************
The source for np.dstack would point the way towards a simpler implementation: array = np.concatenate(map(np.atleast_3d, (arr1, arr2, arr3, arr4, arr5, arr6)), axis=2) 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?
Maybe.
participants (2)
-
Bradley M. Froehle
-
Chao YUE