Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude starting point from linspace function under numpy in python?

I want to exclude starting point from a array in python using numpy. How I can execute? For example I want to exclude 0, but want to continue from the very next real number(i.e want to run from greater than 0) following code x=np.linspace(0,2,10)

like image 417
Moslem Uddin Avatar asked Jun 17 '26 02:06

Moslem Uddin


2 Answers

Kind of an old question but I thought I'll share my solution to the problem.

Assuming you want to get an array

[0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.]

you can make use of the endpoint option in np.linspace() and reverse the direction:

x = np.linspace(2, 0, 10, endpoint=False)[::-1]

the [::-1] reverses the array so that it ends up being in the desired sequence.

like image 111
HaddiWammi Avatar answered Jun 18 '26 19:06

HaddiWammi


x=np.linspace(0,2,10)[1:] #remove the first element by indexing
print(x)
[0.22222222 0.44444444 0.66666667 0.88888889 1.11111111 1.33333333
 1.55555556 1.77777778 2.        ]
like image 32
Bernad Peter Avatar answered Jun 18 '26 19:06

Bernad Peter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!