Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use numpy.repeat:

result = np.repeat(x, rep_size)

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading