Reputation: 3241
import numpy as np
a = np.array([[1,2],
[3,4],
[5,6],
[7,8],
[9,10],
[11,12]])
print np.shape(a)
The expected answer should be:
answer = np.array([[1,2,7,8],
[3,4, 9, 10],
[5,6, 11, 12]])
I tried as
ans = a.reshape(3,-1)
print ans
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
But answer is wrong. How to do it?
Upvotes: 1
Views: 301
Reputation: 31161
I would use split
for this operation:
In [110]: np.hstack(np.split(a,2))
Out[110]:
array([[ 1, 2, 7, 8],
[ 3, 4, 9, 10],
[ 5, 6, 11, 12]])
Upvotes: 0
Reputation: 221534
You could use some reshaping and swapping of axes, like so -
L = 3 # Cutting length
out = a.reshape(-1,L,a.shape[1]).swapaxes(0,1).reshape(L,-1)
Or use np.transpose
to swap the axes, like so -
out = a.reshape(-1,L,a.shape[1]).transpose(1,0,2).reshape(L,-1)
Upvotes: 2