I am trying to concatenate A with C1 and C2. For C1=[], I am not sure why there is an extra [0] in B1. For C2=[1,2], there is a shape mismatch. The current and the desired outputs are attached. I am interested in the following conditions:
(1) If C1=[], no need to insert A1 in B1.
(2) If C1=[1], insert A1 for the specific position in B1.
(3) If C1=[1,2], insert A1 for all the specific positions in B1.
import numpy as np
A=np.array([[[1],
[2],
[3],
[4],
[5],
[6],
[7]]])
C1=[]
C2=[1,2]
D=[7]
A1=np.array([0])
A2=np.array([0])
B1=np.insert(A,C1+D,[A1,A2],axis=1)
print("B1 =",[B1])
B2=np.insert(A,C2+D,[A1,A2],axis=1)
print("B1 =",[B2])
The current output is
B1 = [array([[[1],
[2],
[3],
[4],
[5],
[6],
[7],
[0],
[0]]])]
in <module>
B2=np.insert(A,C2+D,[A1,A2],axis=1)
File "<__array_function__ internals>", line 5, in insert
File "C:\Users\USER\anaconda3\lib\site-packages\numpy\lib\function_base.py", line 4678, in insert
new[tuple(slobj)] = values
ValueError: shape mismatch: value array of shape (2,1) could not be broadcast to indexing result of shape (3,1,1)
The desired output is
B1 = [array([[[1],
[2],
[3],
[4],
[5],
[6],
[7],
[0]]])]
B2 = [array([[[1],
[0],
[0],
[2],
[3],
[4],
[5],
[6],
[7],
[0]]])]