在多维空间中将多个子矩阵重塑/合并为一个矩阵

编程入门 行业动态 更新时间:2024-10-20 20:37:13
本文介绍了在多维空间中将多个子矩阵重塑/合并为一个矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个5D二进制数组'a',其大小为(2、2、4、2、2).该结构如下所示,例如:

I have a 5D binary array 'a' of size (2, 2, 4, 2, 2). The structure looks like this, for example:

a[0,0]: [[[ 0. 1.] [ 0. 0.]] [[ 0. 0.] [ 0. 1.]] [[ 0. 0.] [ 0. 1.]] [[ 0. 0.] [ 1. 0.]]]

我想要做的是制作一个(2,2,4,4)矩阵,该矩阵在最后两个轴上以正方形结构组合2x2矩阵.

What I want to do is to make a (2,2,4,4) matrix that combines the 2x2 matrices in the last two axis, but in a squared structure.

结果应如下所示:

result[0,0]: [[0. 1. 0. 0.] [0. 0. 0. 1.] [0. 0. 0. 0.] [0. 1. 1. 0.]]

我希望这足够清楚.如果将原始矩阵的括号放在结果中,则看起来像这样:

I hope this is clear enough. If I put the brackets of original matrices in results, it looks like this:

result[0,0]: [[[0. 1. [0. 0.] [0. 0.] 0. 1.]] [[0. 0. [0. 0.] [0. 1.] 1. 0.]]]

推荐答案

似乎您正在将第三个轴从(4)分解为(2,2),并将拆分轴的前一半引入到倒数第二个轴位置.因此,有两种方法可以使用 np.reshape 和 ,就像这样-

It seems you are breaking the third axis from (4) into (2,2) and bringing in the first half of the split axes to the second last axes position. So, there are two ways to achieve such an output using np.reshape and np.transpose, like so -

out = a.reshape(2,2,2,2,2,2).transpose(0,1,3,4,2,5).reshape(2,2,4,4) out = a.reshape(2,2,2,-1,2).transpose(0,1,3,2,4).reshape(2,2,4,4)

样品运行-

In [69]: a[0][0] Out[69]: array([[[49, 91], [10, 32]], [[71, 27], [50, 64]], [[ 9, 41], [73, 52]], [[54, 85], [53, 36]]]) In [70]: out1 = a.reshape(2,2,2,2,2,2).transpose(0,1,3,4,2,5).reshape(2,2,4,4) In [71]: out2 = a.reshape(2,2,2,-1,2).transpose(0,1,3,2,4).reshape(2,2,4,4) In [72]: out1[0][0] Out[72]: array([[49, 91, 9, 41], [10, 32, 73, 52], [71, 27, 54, 85], [50, 64, 53, 36]]) In [73]: out2[0][0] Out[73]: array([[49, 91, 9, 41], [10, 32, 73, 52], [71, 27, 54, 85], [50, 64, 53, 36]])

更多推荐

在多维空间中将多个子矩阵重塑/合并为一个矩阵

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

发布评论

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

>www.elefans.com

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