1

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

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

AC(4x3) AA(4x4) AB(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!

3
  • Why do you say concatenate is not optimal? Is it taking very long to process? Or it's inconvenient processing? Commented Jan 10, 2018 at 9:05
  • In the sense that I have to init the 9 sub arrays first and then concatenate like X = np.concatenate(CC,AC) then X = np.concatenate(X,BC) ect... Commented Jan 10, 2018 at 9:07
  • I don't understand the close vote here, the question seems pretty obviously "about programming within the scope defined in the help center." The question would benefit from example code for sure, but...that's worthy of a comment, not a close. Commented Jan 10, 2018 at 9:21

1 Answer 1

3

You want np.block(). It creates an array out of 'blocks', like what you have. For e.g.

>>> 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.]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.