繁体   English   中英

Python - 连接或堆叠两个以上不同形状的数组

[英]Python - Concatenate or stack more than two arrays with different shape

我想获得一个大小为11x11的数组,其中包含不同的子数组,例如由以下数组组成的数组M(括号中的形状):

CC(3x3)CA(3x4)CB(3x4)

AC(4x3)AA(4x4)AB(4x4)

BC(4x3)BA(4x4)BB(4x4)

我可以使用连接但它不是最佳的。 我也尝试过堆栈功能,但是数组必须具有相同的形状。 你有任何想法吗?

非常感谢!

你想要np.block() 它创建了一个“块”阵列,就像你拥有的那样。 例如

>>> CC = 1*np.ones((3, 3))
>>> CA = 2*np.ones((3, 4))
>>> CB = 3*np.ones((3, 4))
>>> AC = 4*np.ones((4, 3))
>>> AA = 5*np.ones((4, 4))
>>> AB = 6*np.ones((4, 4))
>>> BC = 7*np.ones((4, 3))
>>> BA = 8*np.ones((4, 4))
>>> BB = 9*np.ones((4, 4))
>>> M = np.block([[CC, CA, CB],
                  [AC, AA, AB],
                  [BC, BA, BB]])
>>> M
array([[ 1.,  1.,  1.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,  3.],
       [ 1.,  1.,  1.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,  3.],
       [ 1.,  1.,  1.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,  3.],
       [ 4.,  4.,  4.,  5.,  5.,  5.,  5.,  6.,  6.,  6.,  6.],
       [ 4.,  4.,  4.,  5.,  5.,  5.,  5.,  6.,  6.,  6.,  6.],
       [ 4.,  4.,  4.,  5.,  5.,  5.,  5.,  6.,  6.,  6.,  6.],
       [ 4.,  4.,  4.,  5.,  5.,  5.,  5.,  6.,  6.,  6.,  6.],
       [ 7.,  7.,  7.,  8.,  8.,  8.,  8.,  9.,  9.,  9.,  9.],
       [ 7.,  7.,  7.,  8.,  8.,  8.,  8.,  9.,  9.,  9.,  9.],
       [ 7.,  7.,  7.,  8.,  8.,  8.,  8.,  9.,  9.,  9.,  9.],
       [ 7.,  7.,  7.,  8.,  8.,  8.,  8.,  9.,  9.,  9.,  9.]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM