ValientProcess
ValientProcess

Reputation: 1801

Expanding 1D array into multidimensional array using np.repeat

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.

Upvotes: 1

Views: 71

Answers (1)

Divakar
Divakar

Reputation: 221524

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().

Upvotes: 3

Related Questions