How to insert multiple numpy arrays into another numpy array at different indices
Question:
Let’s say we have 4 numpy array A B,C and D, like following:
A = np.array([11,22,33,44,55,66,77])
B = np.array([1,2,3,4,5])
C = np.array([6,7,8,9])
D = np.array([10,11])
I want to insert arrays B, C and D into array A. I have been given a list of indices of array A for where to insert the arrays B,C and D
idx = [1,3,5]
i.e. Array B should be inserted after A[1], array C inserted after A[3] and array D after A[5]
The expected output is something like this:
Out = ([11,22,1,2,3,4,5,33,44,6,7,8,9,55,66,10,11,77])
I have tried using the np.insert
but it does not produce the required output.
Answers:
import numpy as np
A = np.array([11,22,33,44,55,66,77])
B = np.array([1,2,3,4,5])
C = np.array([6,7,8,9])
D = np.array([10,11])
idx = [1,3,5]
# initialize the output array with A
Out = A.copy()
# loop through the indices in reverse order and concatenate the arrays
for i in reversed(idx):
if i == idx[-1]:
Out = np.concatenate((Out[:i+1], D, Out[i+1:]))
elif i == idx[-2]:
Out = np.concatenate((Out[:i+1], C, Out[i+1:]))
else:
Out = np.concatenate((Out[:i+1], B, Out[i+1:]))
print(Out)
One np.insert
doesn’t work because the arrays differ in size. Well it should work with multiple inserts. But since you are starting with lists, list insert will be simpler, and faster.
In [150]: A = ([11,22,33,44,55,66,77])
...: B = ([1,2,3,4,5])
...: C = ([6,7,8,9])
...: D = ([10,11])
Start from the end; that way the size increase won’t mess up the indexing:
In [160]: A[6:6]=D
In [161]: A
Out[161]: [11, 22, 33, 44, 55, 66, 10, 11, 77]
In [162]: A[4:4]=C
In [163]: A[2:2]=B
In [164]: A
Out[164]: [11, 22, 1, 2, 3, 4, 5, 33, 44, 6, 7, 8, 9, 55, 66, 10, 11, 77]
Here’s a way of using one insert:
In [173]: A = np.array([11,22,33,44,55,66,77])
In [174]: idx = np.array([1,3,5]).repeat((len(B),len(C),len(D)))+1
In [175]: idx
Out[175]: array([2, 2, 2, 2, 2, 4, 4, 4, 4, 6, 6])
In [176]: np.insert(A,idx, np.hstack((B,C,D)))
Out[176]:
array([11, 22, 1, 2, 3, 4, 5, 33, 44, 6, 7, 8, 9, 55, 66, 10, 11,
77])
The insert idx
needs to match the values
array in size.
Your [1,3,5]
are ‘insert after’ values. Both list and insert
use ‘insert before’, hence the ‘+1’ adjustment.
Let’s say we have 4 numpy array A B,C and D, like following:
A = np.array([11,22,33,44,55,66,77])
B = np.array([1,2,3,4,5])
C = np.array([6,7,8,9])
D = np.array([10,11])
I want to insert arrays B, C and D into array A. I have been given a list of indices of array A for where to insert the arrays B,C and D
idx = [1,3,5]
i.e. Array B should be inserted after A[1], array C inserted after A[3] and array D after A[5]
The expected output is something like this:
Out = ([11,22,1,2,3,4,5,33,44,6,7,8,9,55,66,10,11,77])
I have tried using the np.insert
but it does not produce the required output.
import numpy as np
A = np.array([11,22,33,44,55,66,77])
B = np.array([1,2,3,4,5])
C = np.array([6,7,8,9])
D = np.array([10,11])
idx = [1,3,5]
# initialize the output array with A
Out = A.copy()
# loop through the indices in reverse order and concatenate the arrays
for i in reversed(idx):
if i == idx[-1]:
Out = np.concatenate((Out[:i+1], D, Out[i+1:]))
elif i == idx[-2]:
Out = np.concatenate((Out[:i+1], C, Out[i+1:]))
else:
Out = np.concatenate((Out[:i+1], B, Out[i+1:]))
print(Out)
One np.insert
doesn’t work because the arrays differ in size. Well it should work with multiple inserts. But since you are starting with lists, list insert will be simpler, and faster.
In [150]: A = ([11,22,33,44,55,66,77])
...: B = ([1,2,3,4,5])
...: C = ([6,7,8,9])
...: D = ([10,11])
Start from the end; that way the size increase won’t mess up the indexing:
In [160]: A[6:6]=D
In [161]: A
Out[161]: [11, 22, 33, 44, 55, 66, 10, 11, 77]
In [162]: A[4:4]=C
In [163]: A[2:2]=B
In [164]: A
Out[164]: [11, 22, 1, 2, 3, 4, 5, 33, 44, 6, 7, 8, 9, 55, 66, 10, 11, 77]
Here’s a way of using one insert:
In [173]: A = np.array([11,22,33,44,55,66,77])
In [174]: idx = np.array([1,3,5]).repeat((len(B),len(C),len(D)))+1
In [175]: idx
Out[175]: array([2, 2, 2, 2, 2, 4, 4, 4, 4, 6, 6])
In [176]: np.insert(A,idx, np.hstack((B,C,D)))
Out[176]:
array([11, 22, 1, 2, 3, 4, 5, 33, 44, 6, 7, 8, 9, 55, 66, 10, 11,
77])
The insert idx
needs to match the values
array in size.
Your [1,3,5]
are ‘insert after’ values. Both list and insert
use ‘insert before’, hence the ‘+1’ adjustment.