隔行2D numpy阵列 [英] Interlace Rows 2D Numpy Array

查看:106
本文介绍了隔行2D numpy阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维的numpy数组,看起来像这样

I have a 2D numpy array that looks like this

array([[x1,x2,x3,x4],
       [x2,x3,x4,x5],
       [x3,x4,x5,x6],
       [y1,y2,y3,y4],
       [y2,y3,y4,y5],
       [y3,y4,y5,y6],])

我想隔行扫描,使数组看起来像这样

I want to interlace the rows such that the array to look like this

array([[x1,x2,x3,x4],
       [y1,y2,y3,y4],
       [x2,x3,x4,x5],
       [y2,y3,y4,y5],
       [x3,x4,x5,x6],
       [y3,y4,y5,y6],])


推荐答案

x = np.random.randint(10,size = (3,5))

Out[23]: 
array([[1, 5, 8, 4, 6],
       [8, 1, 7, 0, 8],
       [4, 7, 5, 2, 9]])

y = np.random.randint(10,size = (3,5))

Out[24]: 
array([[7, 4, 0, 5, 7],
       [4, 2, 8, 6, 1],
       [1, 2, 8, 6, 0]])

np.hstack((x,y)).reshape(6,5)

Out[25]: 
array([[1, 5, 8, 4, 6],
       [7, 4, 0, 5, 7],
       [8, 1, 7, 0, 8],
       [4, 2, 8, 6, 1],
       [4, 7, 5, 2, 9],
       [1, 2, 8, 6, 0]])

这篇关于隔行2D numpy阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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