numpy数组,插入零的交替行 [英] Numpy array, insert alternate rows of zeros

查看:108
本文介绍了numpy数组,插入零的交替行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过在每行之后添加零行来修改NumPy数组.我的矩阵有491行,最后我应该有982行的矩阵.我尝试使用np.repeatnp.tile等多种方法,但无济于事.

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?

推荐答案

您可以通过首先初始化982行数组,然后填充备用行来实现此目的,例如(5列):

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

这篇关于numpy数组,插入零的交替行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆