I have a 1D array A of shape: (35,),
and I want to create from it an array B of shape: (500,35,14,56)
in a way that it will repeat itself 500*14*56 times.
so A[1] = B[0][1][0][0] = B[1][1][1][1] ... etc.
I know I can probably use np.repeat to do it, but I'm not sure how exactly.
1 Answer
Extend input to higher dim and then use np.broadcast_to -
np.broadcast_to(A[:,None,None], (500,35,14,56))
Note that this would be a view into the input. For an output with its own memory space, use .copy().
4 Comments
ValientProcess
Thank you! I tried what you said, but I expected the result of
B[0][:][0][0] give me the values of A, but I get only one of the values from A repeated. Did I do something wrong?Divakar
@ValientProcess Yeah, don't do that. Use the array indexing :
B[0,:,0,0].ValientProcess
Thank you !!! Can you say what's the difference in the indexing?
Divakar
@ValientProcess The first one that you suggested doesn't do what you are expecting it to. To understand what's going on, use
a = np.random.rand(2,3,5) and then do a[0][:][1] and then a[0,:,1]. It's better to try out for yourself to understand.