Python vs Matlab

Updated version at

I believe that in many cases Python can replace Matlab. On this page I will try to explain the differences between Matlab and Python, and explain why I prefer Python. 

Scientific Python distributions

But first a few links to help you get started quickly. You could be coding Python in a matter of minutes!

The easiest way to get started with scientific computing in Python is to use one of the distributions. In that way you automatically get most of the important scientific packages. Some distros also come with an IDE.
  • Pyzo, free distribution based on Python 3, comes with the Interactive Editor for Python (IEP)
  • python(x,y) and WinPython are two free distributions based on the Spyder IDE.
  • Anaconda, Continuum's Python distribution. A free (community) edition is also available.
  • EPD, Enthhoughts Python distribution. A free version is also available

Python compared to Matlab (facts)

Below is a diagram illustrating the main differences between Python and Matlab. Python, by definition, is a programming language. The most common implementation is that in C (also known as CPython) and is what is mostly refered to as "Python". Apart from the programming language and interpreter, Python also consists of an extensive standard library. This library is aimed at progra mming in general and contains modules for os specific stuff, threading, networking, databases, etc.

Matlab is a popular numerical computing environment and programming language, see more on wiki. The concept of Matlab refers to the whole package, including the IDE. The standard library does not contain as much generic programming functionality, but does include matrix algebra and an extensive library for data processing and plotting.

To get similar functionality in Python, you'll need the NumPy, SciPy and Matplotlib packages. Scipy is a package that has the goal of providing all the other functionality of Matlab, including those in the Matlab toolboxes (which would cost you extra in Matlab). Simulink, however, is one example which is not covered in Python. If you depend on it, you should probably stick to Matlab. Maybe in the future a Python alternative will be created.

Additionally, you'll need an IDE. Many pythoneers come from a Linux environment and use a Python shell and one of the many editors, but people coming from Matlab don't usually like this (me included). There are a handful of IDE's available, some of which are for free.

Because Python is open and free, it is very easy for other parties to design packages or other software tools that extend Python. It is possible to create applications using any of the mayor GUI libraries (TK, WX, GTK, QT, ...), use OpenGL, drive your USB port, etc. Another example are pyrex/cython to enhance the speed of algorithms by converting Python to C code, and py2exe and the like to create a standalone application from your source.

So, whats wrong with Matlab?

Not much. I used to love Matlab myself, and I still think it's a good application. However, there are some fundamental issues that made me search for something better. I think the most fundamental problem with Matlab is its commercial nature; it is the basis for several issues:
  • The algorithms are proprietary, which means you can not see the code of the algorithms you are using and have to trust that Matlab implemented it right.
  • Obviously, Matlab is expensive.
  • Since Matlab is a commercial product, money has to be made. This is all right, but making money can become more important than delivering a great product. For instance, Matlab is releasing a new version every 6 months. Each version has to have new/improved features, or people won't buy it. This leads me to question how usefull these features really are, and whether these features could not have been implemented earlier.
  • It makes portability more difficult. The portability solution (the Matlab Component Runtime (MCR)) works fine, but Matlab had to take great care that one cannot use it to do generic Matlabing with it. Maybe this is the reason that the application must be exactly the same version as the installed MCR, which can be a nuisance considering that Matlab releases a new version every 6 months.
  • The proprietary nature also makes it hard, if not impossible, for 3th parties to extend or create tools for Matlab.
Furtheremore, there are some other issues that stem from Matlabs time as a matrix manipulation package:
  • The semicolon. It can be usefull to show the result when you type code in the console, but in scripts or functions I prefer to use disp() when I want to display something.
  • There are a few array indexing issues:
    • Indexing is done with braces rather than brackets;
    • Indexing starts from 1 rather than 0;
    • The fastest changing dimension is in the last dimension.

Advantages of Matlab

Of course, Matlab has its advantages too:
  • It has a solid amount of functions.
  • Simulink is a product which is simply not available elsewhere.
  • It mights also be easier to use for beginners, because the package includes all, while in Python you need to install extra packages and an IDE.
  • It has a large scientific community; it is used on many universities (but few companies have the money to buy a license).

Advantages of Python

My reasons for chosing Python:
  • Free. As in speech and as in beer. (It won't cost you a thing, and you are allowed to view and modify the source.)
  • Beautiful programming language. In my opinion, the python programming language is easier to read and to program than the Matlab programming language. The reason, I think, is because Python was created with the goal of making a beautiful programming language, while Matlab started as a Matrix manipulation package. As I became more familiar with Python, I often was amazed with how well it was designed. There is only one word for that: Beautiful.
  • Powerful. Because it's so well designed, it's easier than other languages to transform your ideas into code. But also, Python comes with extensive standard libraries, and has a powerful datatypes such as lists, sets and dictionaries. These really help to organize your data.
  • Namespaces. Matlab now supports namespaces for the functions that you write, but the core of Matlab is without namespaces; every function is defined in the global namespace. Python works with Modules, which you need to import if you want to use them. (For example import scipy.linalg as la; la.cholesky) Therefore Python starts up in under a second. Using namespaces gives structure to a program and keeps it clean and clear. In Python everything is an object, so each object has a namespace itself. This is one of the reasons Python is so good at introspection.
  • Introspection. This is what follows from the object oriented nature of Python. Because a program has a clear structure, introspection is easy. An example are docstrings: documentation can easily be created in the code right below the definition of a class of function (or at the very start of a module). The documentation of an object (functions are also objects) can be accessed via the .__doc__ property. Note: Matlab has a similar paradigm for inline documentation.
  • String manipulation. This is incredibly easy in Python. What about this line of code which returns a right justified line of 30 characters:
    "I prefer <> over Matlab".replace('<>','Python').rjust(30)
  • Portability. Because Python is for free, your code can run everywhere. And because it's cross platform, it can also works for costumers that run Linux or mac OS X.
  • Indexing. Python indexing goes as it does in C. Firstly, starting from 0, which is easier in most situations, see also here. Secondly, indexing is done using brackets, so you can see the difference between an indexing operation and a function call. You can also select from the end: aList[-2:] returns the last two items in the list.
  • Class and function definitions. Functions and classes can be defined anywhere. In one file (whether it is a module or a script) you can design as many functions and classes as you like. You can even define one in the command line if you really want to...
  • Great GUI toolkits. With Python you can create a front-end for your application that looks good and works well. You can chose any of the major GUI toolkits like WX or QT.

What do other people think ...