简体   繁体   English

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

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

I would like to get an array of size 11x11 with different subarrays, for example the array M composed by the following arrays (shape in parenthesis): 我想获得一个大小为11x11的数组,其中包含不同的子数组,例如由以下数组组成的数组M(括号中的形状):

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

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

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

I could use concatenate but it is not optimal. 我可以使用连接但它不是最佳的。 I also tried the stack function, but arrays must have the same shape. 我也尝试过堆栈功能,但是数组必须具有相同的形状。 Do you have any ideas to do it? 你有任何想法吗?

Thanks a lot! 非常感谢!

You want np.block() . 你想要np.block() It creates an array out of 'blocks', like what you have. 它创建了一个“块”阵列,就像你拥有的那样。 For eg 例如

>>> 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