Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy array, insert alternate rows of zeros

Tags:

python

numpy

I am trying to modify a NumPy array by adding a row of zeros after each row. My matrix has 491 rows and I should end up with a matrix that has 982 rows. I tried multiple ways using np.repeat, np.tile and so on to no avail.

Can anyone help me with this issue?

like image 762
Liviu Avatar asked Nov 25 '14 11:11

Liviu


1 Answers

You could do this by initialising a 982-row array first and then filling alternate rows, for example (5 columns):

a=np.zeros((982,5)) 
b=np.random.randint(0,100,(491,5)) # your 491 row matrix
a[::2] = b
like image 191
atomh33ls Avatar answered Oct 16 '22 17:10

atomh33ls