Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate two arrays in python with alternating the columns in numpy

How to concatenate two arrays in numpy python by taking first column from the first array and fisrt column from the second, then second column from first and second from the other, etc..? that is if I have A=[a1 a2 a3] and B=[b1 b2 b3] I want the resulting array to be [a1 b1 a2 b2 a3 b3]

like image 739
Elena Popa Avatar asked Nov 20 '17 13:11

Elena Popa


1 Answers

Few approaches with stacking could be suggested -

np.vstack((A,B)).ravel('F')
np.stack((A,B)).ravel('F')
np.ravel([A,B],'F')

Sample run -

In [291]: A
Out[291]: array([3, 5, 6])

In [292]: B
Out[292]: array([13, 15, 16])

In [293]: np.vstack((A,B)).ravel('F')
Out[293]: array([ 3, 13,  5, 15,  6, 16])

In [294]: np.ravel([A,B],'F')
Out[294]: array([ 3, 13,  5, 15,  6, 16])
like image 58
Divakar Avatar answered Sep 28 '22 16:09

Divakar