I have numpy 2d array
A = [[1,1,1,1,1],[1,2,3,4,5]]
([[1,1],[1,2]],[[1,1,1],[2,3,4]],[[1,1],[4,5]])
B=[0,1,3,4]
axis=0
def assign_interval(A,B,axis):
if axis == 0:
for i in range(len(B)1):
yield A[:,B[i]:B[i+1]]
else:
for i in range(len(B)1):
yield A[B[i]:B[i+1],:]
list(assign_interval(A,B,0))
[array([[1],
[1]]), array([[1, 1],
[2, 3]]), array([[1],
[4]])]
You could simply use np.split

def assign_interval_split(A,B,axis):
if axis == 0:
return np.split(A[:,B[0]:B[1]],BB[0],axis=1)[1:1]
else:
return np.split(A[B[0]:B[1]],BB[0],axis=0)[1:1]