Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of the Python range function in MATLAB?

Tags:

python

matlab

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.

like image 726
Karen Avatar asked Aug 09 '12 19:08

Karen


People also ask

Is there a range function in Python?

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.

How do you use range PY?

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.


2 Answers

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)).

like image 182
Pursuit Avatar answered Sep 22 '22 00:09

Pursuit


Yes, there is the : operator. The command -10:5:11 would produce the vector [-10, -5, 0, 5, 10];

like image 38
slayton Avatar answered Sep 22 '22 00:09

slayton