Skip to main content
2 of 3
Added "colon" to title for future references
user avatar
user avatar

Python syntax for MATLAB/Octave colon operator a:dx:b

I am trying to rewrite some MATLAB/Octave code in Python, and I don't know what would be the nicest or most intuitive way of writing

octave:10> dt = 0.1;
octave:12> T = 0:dt:1
T =

    0.00000    0.10000    0.20000    0.30000    0.40000    0.50000    0.60000    0.70000    0.80000    0.90000    1.00000
octave:15> dt = 0.17;
octave:16> T = 0:dt:1
T =

    0.00000    0.17000    0.34000    0.51000    0.68000    0.85000

which creates a discretization of the interval [0, 1] with step 0.1, as it's seen. I referred to the NumPy/MATLAB mathesaurus and it uses arange function, but it's not suitable for non-integer values as it's stated in the documentation and shown in this SO question. On the other hand, playing with linspace is not appealing to me because it takes care of endpoints, not spacing.

Which would be a straight-forward, one-line way of doing this in Python?

user782