fnhdx
fnhdx

Reputation: 321

expand 1d numpy array

I want to expand/repeat each element in a 1D array for different times

x = array([7,1,9])  #1d array
rep_size = array([3,2,2]) # repeat number for each element in x
result = arary([7,7,7,1,1,9,9]) #expected result

Is there a numpy function can do this if I don't want to use a for loop. Thanks.

Upvotes: 0

Views: 57

Answers (1)

mozway
mozway

Reputation: 260640

Use numpy.repeat:

result = np.repeat(x, rep_size)

output: array([7, 7, 7, 1, 1, 9, 9])

Upvotes: 1

Related Questions