如何交错numpy.ndarrays?

编程入门 行业动态 更新时间:2024-10-25 15:19:48
本文介绍了如何交错numpy.ndarrays?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在寻找一种可以插入2 numpy.ndarray的方法.这样

I am currently looking for method in which i can interleave 2 numpy.ndarray. such that

>>> a = np.random.rand(5,5) >>> print a [[ 0.83367208 0.29507876 0.41849799 0.58342521 0.81810562] [ 0.31363351 0.69468009 0.14960363 0.7685722 0.56240711] [ 0.49368821 0.46409791 0.09042236 0.68706312 0.98430387] [ 0.21816242 0.87907115 0.49534121 0.60453302 0.75152033] [ 0.10510938 0.55387841 0.37992348 0.6754701 0.27095986]] >>> b = np.random.rand(5,5) >>> print b [[ 0.52237011 0.75242666 0.39895415 0.66519185 0.87043142] [ 0.08624797 0.66193953 0.80640822 0.95403594 0.33977566] [ 0.13789573 0.84868366 0.09734757 0.06010175 0.48043968] [ 0.28871551 0.62186888 0.44603741 0.3351644 0.6417847 ] [ 0.85745394 0.93179792 0.62535765 0.96625077 0.86880908]] >>>

打印c应该将每一行都插入两个矩阵

print c shoule be interleaving each row both matrices

[ 0.83367208 0.52237011 0.29507876 0.75242666 0.41849799 0.39895415 0.58342521 0.66519185 0.81810562 0.87043142]

我一共要交错三个,但是我想一次做两个就容易了.

I have three in total which should be interleaved, but i guess it would be easier to do it two at a time..

但是我如何轻松地做到这一点呢..我读了一些使用数组的方法,但是我不确定使用ndarrays吗?

but how do i do it easily.. I read some method which used arrays, but i am not sure to do it with ndarrays?

推荐答案

使用 np.dstack 并重新调整为 2D -

np.dstack((a,b)).reshape(a.shape[0],-1)

使用三个数组或更多数量的数组,只需添加它们即可.因此,对于三个数组,请使用: np.dstack((a,b,c))并使用第三个数组 c 进行整形.

With three arrays or even more number of arrays, simply add in those. Thus, for three arrays, use : np.dstack((a,b,c)) and reshape with c being the third array.

样品运行-

In [99]: a Out[99]: array([[8, 4, 0, 5, 6], [0, 2, 3, 0, 6], [4, 4, 0, 6, 5], [7, 5, 0, 7, 0], [6, 7, 4, 7, 2]]) In [100]: b Out[100]: array([[3, 5, 8, 6, 5], [5, 6, 8, 8, 4], [8, 3, 3, 3, 5], [2, 1, 1, 1, 3], [5, 7, 7, 5, 7]]) In [101]: np.dstack((a,b)).reshape(a.shape[0],-1) Out[101]: array([[8, 3, 4, 5, 0, 8, 5, 6, 6, 5], [0, 5, 2, 6, 3, 8, 0, 8, 6, 4], [4, 8, 4, 3, 0, 3, 6, 3, 5, 5], [7, 2, 5, 1, 0, 1, 7, 1, 0, 3], [6, 5, 7, 7, 4, 7, 7, 5, 2, 7]])

更多推荐

如何交错numpy.ndarrays?

本文发布于:2023-07-27 00:22:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:numpy   ndarrays

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!