Blog

Matlab versus Python

None

Matlab versus Python

Matlab is a popular numerical computing environment and programming language. 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 that 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(me included). There is 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 major GUI libraries (TK, WX, GTK, QT, ...), use OpenGL, drive your USB port, etc. Another example is 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.

What's wrong with Matlab?

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.

  • 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 useful these features 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 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 3rd parties to extend or create tools for Matlab.

Furthermore, some other issues stem from Matlabs time as a matrix manipulation package:

  • The semicolon. It can be useful 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

Matlab has its advantages included:

  • It has a solid amount of functions.

  • Simulink is a product that is simply not available elsewhere.

  • It might 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

Reasons for choosing 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 is that Python was created to make a beautiful programming language, while Matlab started as a Matrix manipulation package. I often amazed by 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 powerful datatypes such as lists, sets, and dictionaries. These 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 is 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.

  • Portability. Because Python is for free, your code can run everywhere. And because it's cross-platform, it can also work 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. 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 on 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 want to...

  • Great GUI toolkits. With Python, you can create a front-end for your application that looks good and works well. You can choose any of the major GUI toolkits like WX or QT.