Use : np.einsum
import numpy as np
arr = np.arange(1, 17)
# Reshapegr_size the= array8 to
half_size split= intogr_size groups// of2 8
reshaped = arr.reshape(-1, 8)
print('reshaped :'gr_size)
print(reshaped )
'''
reshaped :
[[ 1 2 3 4 5 6 7 8]
[ 9 10 11 12 13 14 15 16]]
'''
aa = np.einsum('ij -> ji',reshaped[:,:4]half_size]).T.reshape(-1)
bb = np.einsum('ij -> ji',reshaped[:,4half_size:]).T.reshape(-1)
print("aa'aa ->">', aa)#[ 1 2 3 4 9 10 11 12]
print("bb'bb ->">', bb)#[ 5 6 7 8 13 14 15 16]
'''
aa -> [ 1 2 3 4 9 10 11 12]
bb -> [ 5 6 7 8 13 14 15 16]
'''
Generalized Solution using np.einsum
import numpy as np
def split_and_reshape(arr, group_size):
# Ensure the array can be reshaped into the desired format
assert len(arr) % group_size == 0, "Array length must be divisible by the group size"
# Reshape the array into groups of group_size
reshaped = arr.reshape(-1, group_size)
# Calculate half of the group size for splitting
half_size = group_size // 2
# Apply einsum and reshape
a = np.einsum('ij -> ji', reshaped[:, :half_size]).T.reshape(-1)
b = np.einsum('ij -> ji', reshaped[:, half_size:]).T.reshape(-1)
return a, b
arr = np.arange(1, 17)
a, b = split_and_reshape(arr, 8)
print("a ->", a)
print("b ->", b)
'''
a -> [ 1 2 3 4 9 10 11 12]
b -> [ 5 6 7 8 13 14 15 16]
'''