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

merge 2D array of 2D arrays into 2D array

I have a 2D array of 2D arrays t, which can be emulated by this code:

import numpy as np

n=3
x = np.random.randint(0,10, (n,1))
y = np.random.randint(0,10, (n,2))

t = np.empty((2,2), object)
t[0,:] = [x,y]
t[1,:] = [x+10,y+10]

How can I merge the t into a single array of size (6,3) like this:

np.vstack([np.hstack([x,y]), np.hstack([x,y])+10])

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 :

One sequence that works:

In [88]: [np.hstack(row) for row in t]
Out[88]: 
[array([[3, 9, 9],
        [1, 1, 0],
        [0, 6, 3]]),
 array([[13, 19, 19],
        [11, 11, 10],
        [10, 16, 13]])]

In [89]: np.concatenate([np.hstack(row) for row in t], axis=0)
Out[89]: 
array([[ 3,  9,  9],
       [ 1,  1,  0],
       [ 0,  6,  3],
       [13, 19, 19],
       [11, 11, 10],
       [10, 16, 13]])
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