1

At first i needed an array like this: [0. , 0.25, 0.5 , 0.75, 1. ] which is np.linspace(0,1,5). But now I need an array like this:

[[0.  ,  0.],
 [0.25,  0.5],
 [0.5 ,  1.],
 [0.75,  1.5],
 [1.  ,  2]]

Note that array[1][0] != array[1][1]!

Can't really explain how but it should sort of like this:

array[:][0] = np.linspace(0,1,5)
array[:][1] = np.linspace(0,2,5)

Where array[0][0] is np.linspace(0,1,5)[0] and array[1][0] is np.linspace(0,1,5)[1]

I hope you kind of understand what array I'm trying to build here. Thanks in advance!

4 Answers 4

1

please notice that your first column is np.linspace(0,1,5) and second one np.linspace(0,2,5) so you can create that array with this two component first make an array from this two with shape 2x5 than when you transpose the array it turns to your 5x2 array:

np.array([np.linspace(0,1,5),np.linspace(0,2,5)]).T
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I needed! Thank you so very much!
0

np.c_ is the numpy function for this

np.c_[np.linspace(0,1,5),np.linspace(0,2,5)]

Output:

       [0.25, 0.5 ],
       [0.5 , 1.  ],
       [0.75, 1.5 ],
       [1.  , 2.  ]])

1 Comment

Hi, I found this to be a much better solution than the one I originally chose to be the answer!
0

Here's one way:

In [33]: start = np.array([0, 0])                                                                                     

In [34]: stop = np.array([1, 2])                                                                                      

In [35]: n = 5                                                                                                        

In [36]: np.linspace(0, 1, n).reshape(-1, 1)*(stop - start) + start                                                      
Out[36]: 
array([[0.  , 0.  ],
       [0.25, 0.5 ],
       [0.5 , 1.  ],
       [0.75, 1.5 ],
       [1.  , 2.  ]])

Comments

0

You second array is just twice of your first array. So you can also consider the following

a = np.linspace(0,1,5)
b = 2*a

np.column_stack((a, b))

# array([[0.  , 0.  ],
#        [0.25, 0.5 ],
#        [0.5 , 1.  ],
#        [0.75, 1.5 ],
#        [1.  , 2.  ]])

Alternatively, you can do either

np.vstack((a,b)).T

OR

np.concatenate((a.reshape(5,1), b.reshape(5,1)), axis=1)

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.