Reshape, permute and reshape
-
In [51]: n = 2 # no. of rows to select from input to form each row of output
In [52]: a.reshape(-1,n,a.shape[1]).swapaxes(1,2).reshape(-1,n*a.shape[1])
Out[52]:
array([[ 1, 2, 5, 6, 9, 10],
[ 3, 4, 17, 8, 11, 12]])
Sort of an explanation :
Cut along the first axis to end up with 3D
array such that we select n
along the second one. Swap this second axis with the last(third) axis, so that we push back that second one to the end.
Reshape to 2D
to merge the last two axes. That's our output!
For more in-depth info, please refer to the linked Q&A.
If we are given the number of rows in output -
In [54]: nrows = 2 # number of rows in output
In [55]: a.reshape(nrows,-1,a.shape[1]).swapaxes(1,2).reshape(nrows,-1)
Out[55]:
array([[ 1, 2, 5, 6, 9, 10],
[ 3, 4, 17, 8, 11, 12]])