Home > Software design >  expand 1d numpy array
expand 1d numpy array

Time:05-19

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.

CodePudding user response:

Use numpy.repeat:

result = np.repeat(x, rep_size)

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

  • Related