2

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]

6 Answers 6

3

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

Comments

2

using transpose of np.vstack and flattening:

np.vstack((arr1, arr2, arr3, arr4)).T.ravel()
# np.array((arr1, arr2, arr3, arr4)).T.ravel()   # alternative way
"""
np.vstack

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

np.vstack().T

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

np.vstack().T.ravel()

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

Comments

1

Try with the following:

np.array([arr1, arr2, arr3, arr4]).T.reshape(1, -1)[0]

Output:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])

Comments

0

I think this is the easiest way to reach your goal.

import numpy as np
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])
newArray = np.array((arr1, arr2, arr3, arr4)).T.ravel()
print(newArray)

what_you_want:

Output :

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

2 Comments

It is same as my answer.
yes .quite close. and you explained it very well. @Ali_Sh
0
    #importing dependencies
    import numpy as np
    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])
    #creating an array to store results
    X = []
    #writing a loop to add each element(i) of the arr1,2,3,4 in the result. but                            
    all the arrays must be of the same lenght as are in your case.
    for i in range (0 , len(arr1)):
      X.append(arr1[i])
      X.append(arr2[i])
      X.append(arr3[i])
      X.append(arr4[i])
    print("X = " , X)  

Comments

-1

You can use np.concatenate like this:

what_you_want = np.concatenate((arr1, arr2, arr3, arr4))

Source: https://numpy.org/doc/stable/user/absolute_beginners.html#adding-removing-and-sorting-elements

You can also look this: https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html?highlight=concatenate#numpy.concatenate

3 Comments

This gives the wrong output, that's why I changed it. @Nesaijn
So you are saying that the best way to go about doing this is to concatenate the arrays, and then sort the list?
I misunderstood the result you wanted. My bad. @AriFrid

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.