question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: equivalent to MATLAB's colon operator

See original GitHub issue

Coming to Python/NumPy from Matlab, I’m missing something like Matlab’s three-argument colon-operator:

>> 0:0.1:1
ans =
  Columns 1 through 3
                         0                       0.1                       0.2
  Columns 4 through 6
                       0.3                       0.4                       0.5
  Columns 7 through 9
                       0.6                       0.7                       0.8
  Columns 10 through 11
                       0.9                         1

In Matlab, I used this operator routinely. It is especially useful when creating a range of values for plotting a function, e.g. x = -1:0.1:1; plot(x, x.^2).

NumPy’s alternatives for this are linspace and arange. However, the former makes it hard to hit specific values (in the plotting example, I have to figure out that there are 21 values), and the latter excludes the stop value. I get that arange follows the behavior of range, but for the mentioned application which has nothing to do with indexing it is just impractical.

I also understand that a naive implementation of the colon operator runs into floating point problems. However, in many years of using Matlab I never ran into a situation where it didn’t do the expected thing. I now managed to find an archived blog post that explains part of their implementation, with an attached complete implementation. I tested it, and as far as I can see it does exactly what the colon operator does.

Would you be open to including an implementation of this algorithm in NumPy? It could be called colon to make it easy for people switching, or irange where the i stands for “inclusive”. For example with a signature irange(start, stop, step=1).

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, May 5, 2020

I think we need to at spend some effort collecting together old bug reports on this, because we keep repeating ourselves in addressing them.

@allefeld, I think a lot of the responses you got here ended up being about the syntactic sugar of the matlab : operator, when really all you cared about was the behavior of the function colon(a, b, c). Sorry about that.

1reaction
allefeldcommented, May 1, 2020

Yes, exactly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

All about the Colon Operator » Loren on the Art of MATLAB
Creating a List of Numbers. You can use the : operator to create a vector of evenly-spaced numbers. Here are the integers from...
Read more >
ECE 3040 Lecture 3: Matlab Basics II
Element-by-element array operations: The “.” operator. • Vector generation with linspace function and “:” (colon) operator. • Graphing data and functions ...
Read more >
MATLAB Tutorial - Engineering Information Technology |
makes the right-hand side of the matrix equations (i.e., A.X) equal the left-hand ... Colon Notation : A central part of the MATLAB...
Read more >
Getting Started With MATLAB
Product enhancement suggestions ... The colon, :, is one of MATLAB's most important operators. ... Why is the magic sum for a 4-by-4...
Read more >
MATLAB vs Python: Why and How to Make the Switch
In this step-by-step tutorial, you'll learn about MATLAB vs Python, why you should ... Creating Arrays Is Very Flexible in NumPy; The Colon...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found