Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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)
).
Others have mentioned the colon
operator. You just have to be aware of some differences.
In Python, range
takes all integer parameters and returns an integer list. In MATLAB, the colon operator can handle floating point in both the start/stop as well as the step size.
I would say that numpy.arange
is a closer match to MATLAB's colon operator.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.