← Back

I used Matlab. Now I use Python.

Posted on 25 September 2010.

Colleagues have asked me why I changed from Matlab to Python, and what makes Python so great. For example, a friend recently asked the following:

I noticed that the use of python to do signal processing tasks is slowly increasing (saw it mentioned in some papers). I am curious to know what are the advantages/disadvantages of python vis-a-vis C or Matlab. What do you think are the main pros and cons that have led you to use python?

Because this precise question — why Python over Matlab? — has become so popular, I thought I would offer my reasons for switching.

  1. Python is totally free. Matlab is not. Undergrads living on campus are able to get Matlab for free through… ahem… alternative means. As a grad student disconnected from on-campus life, it is hard for me to use such… ahem… alternative means to get a copy of Matlab, especially for Linux.
  2. Python is a general-purpose language, i.e., it is used for scientific computing, enterprise software, web design, back end, front end, and everything in between. Matlab is purely for scientific computing, although it does a great job at it.
  3. Python syntax is beautiful. Once you get over the use of meaningful whitespace, you realize how much it makes sense. Famous entrepreneur and investor Paul Graham mentions a friend who uses Python because “he likes the way source code looks.” Graham explains further:
  4. That may seem a frivolous reason to choose one language over another. But it is not so frivolous as it sounds: when you program, you spend more time reading code than writing it. You push blobs of source code around the way a sculptor does blobs of clay. So a language that makes source code ugly is maddening to an exacting programmer, as clay full of lumps would be to a sculptor.
  5. Python is inherently object oriented. Almost everything is an object: strings, lists, dictionaries, tuples, functions, classes, and more. The implied usefulness is that these things each have their own members and methods that encapsulate its functionality and information.
  6. Python is high level, easy to learn, and fast to develop. (So is Matlab.) Program scripts are easily profiled and debugged. Although C++ may save you one minute of computation, Python may save you one month of development.
  7. Python has so many cool features that makes tasks that are difficult in other languages easy. For example, see these features.
  8. Python is fast enough. Bottlenecks can be addressed by writing a bit of C (something I have not yet needed to do), or you can parallelize your code easily in Python. For what it’s worth, Python was written in C.
  9. Python is popular and has a great community among programmers, scientists, mathematicians, and engineers, making it easy to find help on the Internet. At the moment, “Python” is the tenth most popular tag on StackOverflow (which, by the way, may be my favorite site on the Internet), and Python is the second most popular language among Project Euler members.
  10. Python has great libraries. It also makes any library you want already available. Want something that does hierarchical clustering? There are a bunch of Python packages that do exactly that.
  11. Python packages can do nearly everything Matlab can do for signal processing. NumPy is like the Matlab core, SciPy is like Matlab toolboxes, Matplotlib lets you print pretty graphs, and IPython emulates the Matlab desktop environment. Over the years, additions and revisions to NumPy/SciPy were written to make Matlab users comfortable, e.g., PyLab. Many sites offer help regarding the transition from Matlab to NumPy/SciPy, such as this.
  12. The demand for Python programmers is increasing. Many large companies (e.g., Google, Facebook) and small startups are hiring people with Python experience. Even the finance industry is interested. The U.S. Securities and Exchange Commission recently proposed a mandate that would require securities issuers to submit a computer program that maps the logic flow of funds, and that “this computer program be filed… in Python”.

Disadvantages? Python is probably easier to learn from scratch than C++, but it still takes time. If you are already comfortable with Matlab, and you have Matlab, then you should keep using Matlab. But if you want a language that is free, flexible, popular, and may make you more marketable, then give Python a try. Believe me, I like Matlab. I just like Python better.

10 Comments (old comments from Wordpress)

1. Comment by Malcolm Slaney

I agree with you, except for two things.

1) the graphics for Numpy suck. It’s still much easier to make a useful graph in Matlab.

2) The indentation in Python makes it hard to try out little pieces of code as I develop something new.

3) Oh, yes, the debugging environment in Python leaves a lot to be desired.

Matlab is far from perfect (it’s just Fortran after all :-) ). So I use both.

My latest system uses Python for lots of stuff (mostly because of the text and web tools). But I use Matlab for debugging. My Python application has a web API that can output HTML, JSON or Matlab code (for the results). urlopen() works really well to talk to a web application and all is well!

- Malcolm

January 21, 2011 at 3:49 pm

2. Comment by Steve

Thank you for the comment. I loved the Matlab profiler and relied on it heavily. Python’s profiler and debugger are usable, but as you pointed out, it’s just a bit easier with Matlab’s interface.

January 21, 2011 at 8:10 pm

3. Comment by steveha

The best debugging environment I have seen for Python is the Wing IDE. It lets you single-step in your Python code.


February 15, 2011 at 2:43 pm

4. Comment by Colin

Re: Malcom’s comments, for graphing, I’ve found matplotlib able to do everything that Matlab can do and then some – and often the graphics look significantly better. With the obvious glaring exception of 3d graphs! In terms of indentation making things hard to test, I’m guessing you’re referring to the fact that if you have a bunch of source code (example code or code you’ve written) and you want to try to run a small part of it, then you can’t directly copy-paste it into the python prompt. A very simple fix for this (albeit a tiny bit kludgy) is just to enter if 1: into the prompt prior to pasting. This allows you to start at any arbitrary indentation amount in your code, and since python doesn’t care if the indentation amount is uniform, you can copy/paste directly. Hope that’s what you were referring to…

May 17, 2011 at 7:02 pm

5. Comment by Siyi Deng

The syntactic aesthetics is something totally subjective, and I generally like matlab’s syntax better. Python’s indentation is awkward in some cases.

a simple example:

a(end); % Matlab syntax, straitforward, learners can easily guess what this is.

a[-1] # Python syntax. Not very new user friendly imho.

July 7, 2011 at 8:50 pm

6. Comment by Yoda

Regarding 3D graphics in Python: an excellent for this is MayaVi2. It includes a package called ‘mlab’ which provides a simple way to produce 3D plots — using commands that are often identical to matlab.

July 22, 2011 at 7:48 am

7. Comment by Matt

I’ve used Matlab for years but have recently been primarily using Python/Numpy and Cython with the WingIde. The two primary drivers have been cost and speed. Python numpy code can be coaxed to run much faster than Matlab via profiling and compiling to C via Cython. Moreover I now have an extensive CUDA library hooked into Python so it generally runs faster than vanilla C.

However Numpy is not quite as productive as Matlab for scientific computing, though it could easily have been as productive as Matlab if not for a few key design mistakes in numpy. The problem boils down to one major issue, the fact that array slicing reduces/changes the dimension of the array. Thus if M is a 3 x 2 matrix, M[0,:] is no longer a matrix but a 2-D vector. It should instead be a 1 x 2 matrix and assignment should have sensible broadcast rules to handle singleton dimensions.

This will generate endless bugs, makes the interface into the well written matplotlib library persnickety, riddle your code with reshape calls and generate many other annoyances. In theory the matrix subclass of the array class is better behaved, but most functions in the library don’t use that class so you are forced to constantly convert.

There are also some other gotcha’s like the fact that numpy hates Fortran column ordering even though that format is assumed for most external Fortran and even C math libraries. Also numpy arrays don’t overload any operators to handle standard matrix multiplies. There is no decent \ or inv() function without jumping with a bunch of syntactic hooks etc.

August 22, 2011 at 8:35 pm

8. Comment by Steve

1. Yeah, the change in dimension via array slicing is sometimes annoying. Here’s a common example: reading audio signals. A stereo signal has shape (N, 2), but a mono signal has shape (N,), not (N, 1). So then if you try to check X.shape[1], it gives an IndexError.

2. Operator overloading is an obvious advantage in Matlab, but I’ve gotten so used to scipy.linalg that it’s not a big deal anymore.

3. You can adjust the ordering through scipy.array(…, order=’F'). You’re right — it does make a huge difference in execution time.

August 22, 2011 at 9:19 pm

9. Comment by joon

“a(end); % Matlab syntax, straitforward, learners can easily guess what this is.”

Actually, one does not know if a is a variable or a function. Using () for both function argument and indexing is very confusing. Moreover, you can call a function without any () in matlab, which makes it more confusing.

January 18, 2012 at 9:46 am

10. Comment by ZyX

> a[-1] # Python syntax. Not very new user friendly imho.

But programmers friendly: there are lots of modern languages that use negative indexes for accessing list items from their end. What is not common (at least, for me, with background of Perl, VimL and zsh that all have different behavior here) is that for a=[0, 1, 2] "a[0:1]" returns just "[0]", and same is range: "range(0, 3)" is "[0, 1, 2]".

> 2. Operator overloading is an obvious advantage in Matlab, but I've gotten so used to scipy.linalg that it's not a big deal anymore.

Operator overloading is present in python, though names of functions that are to be defined in order to overload them are not so obvious as in C++. Even less is overloading in python modules written in C.

May 25, 2012 at 12:46 pm