问题描述
我有一个像这样的 3d 矩阵
I have a 3d matrix like this
arange(16).reshape((4,2,2))
array([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15]]])
并希望以网格格式将它们堆叠起来,以
and would like to stack them in grid format, ending up with
array([[ 0, 1, 4, 5],
[ 2, 3, 6, 7],
[ 8, 9, 12, 13],
[10, 11, 14, 15]])
有没有一种方法可以不显式地对它们进行 hstacking(和/或 vstacking)或添加额外的维度和整形(不确定这是否可行)?
Is there a way of doing without explicitly hstacking (and/or vstacking) them or adding an extra dimension and reshaping (not sure this would work)?
谢谢,
推荐答案
In [27]: x = np.arange(16).reshape((4,2,2))
In [28]: x.reshape(2,2,2,2).swapaxes(1,2).reshape(4,-1)
Out[28]:
array([[ 0, 1, 4, 5],
[ 2, 3, 6, 7],
[ 8, 9, 12, 13],
[10, 11, 14, 15]])
<小时>
我在此处发布了更多用于将数组整形/取消整形为块的通用函数.
这篇关于3d Numpy 数组到 2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2