How to create range of numbers in Python like in MATLAB
Asked Answered
T

5

13

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]

Thornie answered 30/6, 2015 at 16:36 Comment(3)
use the range functionMclin
In the likely event that you're using numpy, there's a similar arange; note that range and arange are both half-open, they exclude the stop (e.g. np.arange(1, 10, 0.5) will be array([ 1. , 1.5, 2. , ... , 8.5, 9. , 9.5])).Jollify
range didn't work with floating increment, if i use np.arange, then how to include the increment after?Thornie
Z
9

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

Zoogloea answered 30/6, 2015 at 19:11 Comment(0)
A
6

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]
Advertise answered 30/6, 2015 at 16:48 Comment(1)
miss parenthesis on 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
B
2
import numpy as np
a = np.arange(1, 10, 0.5)
print (a)
Bonaire answered 30/6, 2015 at 16:40 Comment(2)
Why would you convert the array back to a list (and, if you were particularly determined to do so, why [a for a in ...] rather than list(...))?!Jollify
I guess he said without using loops.Mclin
S
1
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
Sabbatarian answered 12/2 at 14:12 Comment(1)
This is not exactly how the colon operator works in MATLAB. It does something clever to use the end value even if rounding errors make the end value just outside the range. See here: https://mcmap.net/q/587908/-how-does-the-colon-operator-work-in-matlab — still, nice answer.Kazak
C
0

Python's built in xrange function can generate integers like so:

xrange(start, stop, step)

But xrange cannot do floats.

Cistern answered 30/6, 2015 at 16:46 Comment(1)
You can with the proper workaround: #477986 .. however, numpy.arange is more elegant.Compatible

© 2022 - 2024 — McMap. All rights reserved.