Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stretch, scale, or double up an array with numpy?

I was wondering if there is a numpy function to "stretch" an array along a specific axis like the following:

a =[[1,2,3,4],[1,2,3,4]]

to

a = [[1,1,2,2,3,3,4,4],[1,1,2,2,3,3,4,4]]

Thanks in advance!

like image 644
timkado Avatar asked Mar 12 '26 10:03

timkado


1 Answers

import numpy as np
a = np.array([[1,2,3,4],[1,2,3,4]])

First possibility:

a.repeat(2, axis=1)

or the second:

np.kron(a, [1,1])

Both returning:

array([[1, 1, 2, 2, 3, 3, 4, 4],
       [1, 1, 2, 2, 3, 3, 4, 4]])
like image 160
eumiro Avatar answered Mar 13 '26 22:03

eumiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!