Is there any way to create a range of numbers in Python like MATLAB using a simple syntax, i.e, not using loops. For example:
MATLAB:
a = 1:0.5:10
give
a = [1 1.5 2 2.5 3 3.5 .... 9.5 10]
Is there any way to create a range of numbers in Python like MATLAB using a simple syntax, i.e, not using loops. For example:
MATLAB:
a = 1:0.5:10
give
a = [1 1.5 2 2.5 3 3.5 .... 9.5 10]
As others have pointed out, np.arange
gets you closest to what you are used to from matlab. However, np.arange
excludes the end point. The solution that you proposed in your own answer can lead to wrong results (see my comment).
This however, will always work:
start = 0
stop = 3
step = 0.5
a = np.arange(start, stop+step, step)
For further reading: Especially if you are an experienced matlab-user, this guide/cheat sheet might be interesting: Link
Numpy has arange
and r_
which look something like this:
import numpy as np
print(np.arange(1, 3, .5))
# [ 1. 1.5 2. 2.5]
print(np.r_[1:3:.5])
# [ 1. 1.5 2. 2.5]
Notice that it is a little different than matlab, first the order of the stop and step are reversed in numpy compared to matlab, and second the stop is not included the the result. You might also consider using linspace
it's often preferred over arange
when you're working with floating point numbers because num
can be defined more precisely than step
:
print(np.linspace(1, 3, num=5))
# [ 1. 1.5 2. 2.5 3. ]
or
print(np.linspace(1, 3, num=4, endpoint=False))
# [ 1. 1.5 2. 2.5]
print(np.linspace(1, 3, num=5)
may be print(np.linspace(1, 3, num=5))
but thanks!!! +1. i solve my problem using np.append(np.arange(start, stop, step),stop)
–
Thornie import numpy as np
a = np.arange(1, 10, 0.5)
print (a)
array
back to a list
(and, if you were particularly determined to do so, why [a for a in ...]
rather than list(...)
)?! –
Jollify a = np.arange(start, stop+step, step)
doesn't work for all cases!
I explain :
Octave (Matlab) version gives 9 values for this case:
clc
start=0
stop=1.2
step=1/7
a = start : step : stop
length(a)
It gives:
start = 0
stop = 1.2000
step = 0.1429
a =
Columns 1 through 8:
0 0.1429 0.2857 0.4286 0.5714 0.7143 0.8571 1.0000
Column 9:
1.1429
ans = 9
It gives 9 values.
This Python version gives 10 values:
start = 0
stop = 1.2
step = 1 / 7
print("step", step)
a = np.arange(start, stop + step, step)
print(a)
print(a.size)
gives
step 0.14285714285714285
[0. 0.14285714 0.28571429 0.42857143 0.57142857 0.71428571
0.85714286 1. 1.14285714 1.28571429]
10
(Note how 1.28571429 > 1.2)
The solution:
def arange_like_matlab_or_octave(a, b, srate):
k = []
i = 0
dt = 1 / srate
t = a + i * dt
while t <= b:
k.append(t)
i += 1
t = a + i * dt
return np.array(k)
with this:
a = arange_like_matlab_or_octave(start, stop, 1 / step)
It gives a numpy array like np.arange
:
[0. 0.14285714 0.28571429 0.42857143 0.57142857 0.71428571
0.85714286 1. 1.14285714]
9
Python's built in xrange
function can generate integers like so:
xrange(start, stop, step)
But xrange
cannot do floats.
numpy.arange
is more elegant. –
Compatible © 2022 - 2024 — McMap. All rights reserved.
range
function – Mclinnumpy
, there's a similararange
; note thatrange
andarange
are both half-open, they exclude thestop
(e.g.np.arange(1, 10, 0.5)
will bearray([ 1. , 1.5, 2. , ... , 8.5, 9. , 9.5])
). – Jollify