本文介绍了将2D阵列合并为3D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我有3个numpy数组,如下所示.
Hello I have 3 numpy arrays as given below.
>>> print A
[[ 1. 0. 0.]
[ 3. 0. 0.]
[ 5. 2. 0.]
[ 2. 0. 0.]
[ 1. 2. 1.]]
>>> print B
[[ 5. 9. 9.]
[ 37. 8. 9.]
[ 49. 8. 3.]
[ 3. 3. 1.]
[ 4. 4. 5.]]
>>>
>>> print C
[[ 0. 0. 0.]
[ 0. 6. 0.]
[ 1. 4. 6.]
[ 6. 2. 0.]
[ 0. 5. 4.]]
我想将它们合并为
[[[ 1. 0. 0.]
[ 5. 9. 9.]
[ 0. 0. 0.]]
[[ 3. 0. 0.]
[ 37. 8. 9.]
[ 0. 6. 0.]]
[[ 5. 2. 0.]
[ 49. 8. 3.]
[ 1. 4. 6.]]
[[ 2. 0. 0.]
[ 3. 3. 1.]
[ 6. 2. 0.]]
[[ 1. 2. 1.]
[ 4. 4. 5.]
[ 0. 5. 4.]]]
那是我想从每个数组中取一行.有人可以告诉我一种简单的方法吗?我已经尝试过hstack
,vstack
.但是他们没有给出理想的结果.
That is I would like to take one row from each array.Could anyone tell me a simple way to do it?I already tried hstack
, vstack
. But they are not giving the desired result.
谢谢!
推荐答案
使用numpy的解决方案 dstack
:
A solution using numpy dstack
:
>>> import numpy as np
>>> np.dstack((a,b,c)).swapaxes(1,2)
array([[[ 1, 0, 0],
[ 5, 9, 9],
[ 0, 0, 0]],
[[ 3, 0, 0],
[37, 8, 9],
[ 0, 6, 0]],
[[ 5, 2, 0],
[49, 8, 3],
[ 1, 4, 6]],
[[ 2, 0, 0],
[ 3, 3, 1],
[ 6, 2, 0]],
[[ 1, 2, 1],
[ 4, 4, 5],
[ 0, 5, 4]]])
这篇关于将2D阵列合并为3D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!