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

Insert an array into another array in numpy?

How do I do this? (in a computationally efficient way)

arr1 = np.array([0, 4, 8, 12, 16])
arr2 = np.array([1, 5, 9, 13, 17])
arr3 = np.array([2, 6, 10, 14, 18])
arr4 = np.array([3, 7, 11, 15, 19])

what_i_want = [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

>Solution :

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

You can directly pass a list of arrays to np.ravel to flatten in Fortran order:

np.ravel([arr1, arr2, arr3, arr4], 'F')

Slower alternative with stack and ravel:

np.stack([arr1, arr2, arr3, arr4]).ravel('F')

output:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
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