Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Expand a multidimentional array with another array of different shape

I have the following arrays:

A = np.array([
            [[[0, 1, 2, 3],
              [3, 0, 1, 2],
              [2, 3, 0, 1],
              [1, 3, 2, 1],
              [1, 2, 3, 0]]],
            
            [[[9, 8, 7, 6],
              [5, 4, 3, 2],
              [0, 9, 8, 3],
              [1, 9, 2, 3],
              [1, 0, -1, 2]]],
            
            [[[0, 7, 1, 2],
              [1, 2, 1, 0],
              [0, 2, 0, 7],
              [-1, 3, 0, 1],
              [1, 0, 1, 0]]]
              ])

A.shape
(3,1,5,4)

B = np.array([
    [[[1, 0],
      [-1, 2],
      [9, 1],
      [8, 2],
      [7, 0]]],
            
      [[[9, 6],
       [5, 2],
       [0, 3],
       [1, 9],
       [1, 0]]],
            
      [[[0, 7],
        [1, 0],
        [0, 7],
        [-1, 1],
        [0, 0]]]
])

B.shape
(3,1,5,2)

Then I want to expand array A with B in the last dimension of A. Such that, the result X is:

X = np.array([
            [[[0, 1, 2, 3, 1, 0],
              [3, 0, 1, 2,-1, 2],
              [2, 3, 0, 1, 9, 1],
              [1, 3, 2, 1, 8, 2],
              [1, 2, 3, 0, 7, 0]]],
            
            [[[9, 8, 7, 6, 9, 6],
              [5, 4, 3, 2, 5, 2],
              [0, 9, 8, 3, 0, 3],
              [1, 9, 2, 3, 1, 9],
              [1, 0,-1, 2, 1, 0]]],
            
            [[[0, 7, 1, 2, 0, 7],
              [1, 2, 1, 0, 1, 0],
              [0, 2, 0, 7, 0, 7],
              [-1,3, 0, 1,-1, 1],
              [1, 0, 1, 0, 0, 0]]]
              ])

X.shape
(3,1,5,6)
``

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to concatenate the 2 arrays together along the axis you need:

C = np.concatenate((A, B), axis=3)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading