Is there an equivalent MATLAB function for the range()
function in Python?
I'd really like to be able to type something like range(-10, 11, 5)
and get back [-10, -5, 0, 5, 10]
instead of having to write out the entire range by hand.
Python range() Function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
Syntax of range()start: integer starting from which the sequence of integers is to be returned. stop: integer before which the sequence of integers is to be returned. The range of integers ends at stop – 1. step: integer value which determines the increment between each integer in the sequence.
There are two relevant functions. The colon :
operator, you can use the linspace
function. The best function to use depends on what you want to specify.
Examples:
x = -10:5:10; % Count by 5's from -10 to 10. (or "colon(-10, 5, 10)")
x = linspace(-10, 10, 5); % 5 even increments between -10 and 10
The result of the colon
operator will always include the first argument and the desired spacing, but generally will not include the last argument. (e.g. x = -10:5:11
).
The linspace
function will always include the desired first and last elements, but will the element spacing will vary. (e.g. linspace(-10, 11, 5)
).
Yes, there is the :
operator. The command -10:5:11
would produce the vector [-10, -5, 0, 5, 10];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With