Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从子矩阵形成矩阵?_Python_Arrays_Numpy_Matrix_Vector - Fatal编程技术网

Python 如何从子矩阵形成矩阵?

Python 如何从子矩阵形成矩阵?,python,arrays,numpy,matrix,vector,Python,Arrays,Numpy,Matrix,Vector,假设我有以下4个子矩阵: print(A[0]) print(A[1]) print(A[2]) print(A[3]) [[ 0. 1. 2.] [ 6. 7. 8.] [ 12. 13. 14.]] [[ 3. 4. 5.] [ 9. 10. 11.] [ 15. 16. 17.]] [[ 18. 19. 20.] [ 24. 25. 26.] [ 30. 31. 32.]] [[ 21. 22. 23.] [ 27

假设我有以下4个子矩阵:

print(A[0])
print(A[1])
print(A[2])
print(A[3])

[[  0.   1.   2.]
 [  6.   7.   8.]
 [ 12.  13.  14.]]
[[  3.   4.   5.]
 [  9.  10.  11.]
 [ 15.  16.  17.]]
[[ 18.  19.  20.]
 [ 24.  25.  26.]
 [ 30.  31.  32.]]
[[ 21.  22.  23.]
 [ 27.  28.  29.]
 [ 33.  34.  35.]]
根据该矩阵:

[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]
 [24 25 26 27 28 29]
 [30 31 32 33 34 35]]

如何管理子矩阵以复制原始子矩阵?

使用
A
作为包含这些子矩阵的输入数组,您可以使用一些
整形
和置换维度,如下所示-

A.reshape(2,2,3,3).transpose(0,2,1,3).reshape(-1,6)
m,n,r = A.shape
out = A.reshape(-1,2,n,r).transpose(0,2,1,3).reshape(-1,2*r)
解释-

  • 将第一个轴切割成两部分:
    A.重塑(2,2,3,3)
  • 交换第二和第三轴:
    。转置(0,2,1,3)
  • 最后,重塑为具有
    6
    列:
    。重塑(-1,6)
因此,这个解可以这样推广-

A.reshape(2,2,3,3).transpose(0,2,1,3).reshape(-1,6)
m,n,r = A.shape
out = A.reshape(-1,2,n,r).transpose(0,2,1,3).reshape(-1,2*r)
样本运行

让我们重新创建您的
A
-

In [54]: A = np.arange(36).reshape(-1,3,2,3). transpose(0,2,1,3).reshape(4,3,3)

In [55]: A
Out[55]: 
array([[[ 0,  1,  2],
        [ 6,  7,  8],
        [12, 13, 14]],

       [[ 3,  4,  5],
        [ 9, 10, 11],
        [15, 16, 17]],

       [[18, 19, 20],
        [24, 25, 26],
        [30, 31, 32]],

       [[21, 22, 23],
        [27, 28, 29],
        [33, 34, 35]]])
然后,运行我们的代码-

In [56]: A.reshape(2,2,3,3).transpose(0,2,1,3).reshape(-1,6)
Out[56]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

你能给出矩阵的定义,让我们来玩玩它吗?你能说出你的一般问题吗?因为否则你可以使用my_mat=np.arange(36)。重塑((6,6))。你的矩阵是np.vstack((np.hstack((A[0],A[1])),np.hstack(A[2],A[3]))