Is Python better than MATLAB

welsch.ONE Michael Welsch's blog

Oct 05, 2016 No comments

The following comparison is not intended to fuel a fanboy debate à la Xbox is better than Playstation or PC. In general, nothing speaks against using both Matlab and Python in parallel. True to the motto, the right tool for every task. But of course the motto only applies to those who know the tools. After all, you have to start with some kind of program.

With both programs you are in good hands for getting started with numerical computing, because both Python and Matlab are programs that can be used by beginners and professionals for a variety of numerical problems, as they offer extensive function libraries and are easy to program. However, they may not be particularly efficient in the respective applications.

The idea of ​​the right tool for the respective application is therefore pursued more extensively by professionals. Depending on the algorithm, programming is then carried out close to the hardware on clusters, graphics cards or FPGA. In principle, the question is not whether you should start numerical programming with Excel, Matlab / Python, Java, C / C ++, Fortran or Assembler. Compared to these differences, you can almost flip a coin when deciding between Python or Matlab.

As a beginner, you don't have to deal with programming-related things in Python and Matlab that, as a beginner, you usually want to be spared. As a beginner, the goal is often to calculate something that is too time-consuming to do a manual calculation. You might want to calculate something non-linear that you could only find in the textbook for the linear case so far. You want to calculate an equation and not immediately develop the most efficient solution method for it. Efficiency is not critical - as long as it runs overnight. However, one does not want to deal with memory addressing, data formats, etc. under any circumstances.

So, the question of whether you should use Matlab or Python as an introduction to numerical computing is not about what is generally optimal for numerical computing.

costs
Matlab, including the good toolboxes, costs a few thousand euros in license fees. Python is generally free. Most technical universities, however, have academic licenses for Matlab including all toolboxes.

Started
A distinction must be made here whether one has programming experience or not. Anyone who has no experience in dealing with object-oriented programming will find their way around faster in Matlab with the consistent vector and matrix approach. In addition, Matlab offers a development environment, i.e. a main program in which one works, in which, in addition to the code, the variables and plots are also in constant view and can even be changed interactively. In addition to interactive data import or the plot function, there are also small, ready-made apps such as the Curve-Fitting App or the Distribution-Fitting App for many applications. So you can first simply click together evaluations etc. and then have the corresponding Matlab code created for them. You can quickly understand how functionalities can be implemented in a program. You can then automatically run the code repeatedly or expand it individually. In any case, you don't start with a blank screen, but with executable code, even if it's just with an example from the documentation or the tutorials that are available for many scientific disciplines.

Anyone who has already acquired extensive knowledge of programming and programmed in a full-fledged development environment or in a powerful editor will be more likely to find their way around Python, and will then be spoiled for choice between a wide variety of environments and editors. Anyone who has got used to color highlighting, autocompletion and code management in Visual Studio, for example, will find the Matlab development environment to be an anachronistic step backwards.

Matlab is ready for use immediately after installing a file. For Python there are also ready-to-install distributions such as Anakonda, but usually you still have to set environment variables manually here and there, install plug-ins for the development environment, set up compilers, etc.
If you want to start with the numerical calculation of scientific algorithms and have little programming experience and do not necessarily want to acquire it, you will be much faster with Matlab than with Python. For experienced programmers, however, the comparatively easy familiarization with Matlab is not a big selling point.

Program
Both Matlab and Python are highly developed languages. This means that they offer complex data structures such as arrays, lists and tables in which you can save your data for processing. They provide corresponding methods (functions) for the data types. So you don't have to worry about how the data is organized in memory and in many cases you don't even have to worry about defining the data type of the variable. Integer and double, for example, are automatically converted if necessary. Only in the case of characters, i.e. everything that are non-calculable numbers, a transformation is necessary in both languages, which can be done quickly and easily using double2str commands, for example. When it comes to processing strings, Python has a clear advantage. However, word processing and editing are not of great importance for scientific arithmetic.

Both languages ​​are interpreter languages. The code is compiled line by line, so you can also run the code line by line and view and manipulate the variables in a separate window without laboriously outputting or debugging intermediate values ​​via code. Interpreter language also means that you can write scripts, which in turn can write scripts that are then executed. Variants of the calculation model can be automatically created as different functions via script and then executed in a batch process.

Matlab and Python (or Python + numpy, because only this additional library integrates the array functionality in Python, comparable to Matlab) unfortunately differ in the handling of vectors and matrices.

If you want to read or save values ​​from a matrix, the first entry of a row or column is addressed as 0 in Python. This is extremely irritating for scientific programming and leads to errors, since it does not correspond to what you write with pen and paper. Matlab starts here consistently and intuitively with the 1. On the other hand, one usually uses iterable lists in Python and then simply writes that the command should be carried out for all elements of the list. In this case, you don't even get into the position of having to address the first element directly using an integer. Overall, the Python language, apart from the annoyance with the zero, is a bit smarter or the code corresponds in a more natural way to what you would tell the computer in English what it should do. The Matlab code is more compact in terms of purely numerical operations.

Fiddling with curly brackets (as in C) is spared in both languages. Python even recognizes loops and instructions just by indenting the following lines. A previous disadvantage of Matlab compared to Python was that functions cannot be defined on-the-fly in scripts. But that was a little leveled out recently with R2016b. Functions can now at least be defined at the end of a script. In addition, from R2016a onwards, Matlab offers the live script format, with which code, explanations, images, links and mathematical formulas (using latex syntax) can be prepared in one document very comfortably and clearly. This makes it much easier to document the scientific work and is much clearer than doing it with comments and extra documents. This functionality has been available in Python as a web interface for a while, but it is not implemented quite as convincingly.

Documentation and help
The situation here is really simple. In contrast to Python, Matlab offers documentation that is more professional, more readable, more uniform, cross-linked and more extensive. Finally, in Python, the developers of the individual modules themselves document on different websites what works sometimes more and sometimes less well. In the Matlab documentation and on the company's Internet portal there are countless examples of how the functions can be used. In contrast, the Python community on the Internet seems to be much larger, but also more heterogeneous and not only specialized in scientific computing. It's difficult to quantify. Help from the net is very good for both programs, I would say.

Functionality provided
Both programs provide the typical basic functionalities for getting started with scientific computing. However, the out-of-the-box functionality of Matlab is heavily dependent on the toolboxes acquired with it, which also offer a lot. In Python, on the other hand, you can download many libraries for free from the community at the push of a button. Here you might find one or the other special function that Matlab does not offer. In the Matlab community, however, extensive special functions or compiled toolboxes have been offered for download via a forum with a review system for a long time.

However, one functional aspect can really be decisive when deciding between Matlab and Python.

There is no adequate substitute for the Simulink toolbox from Maltab in Python. Simulink is really powerful and, thanks to its visual representation of information flows and operators, is very intuitive for a wide variety of applications, for example for solving non-linear differential equations and for modeling physical systems including the design of suitable control strategies, e.g. as hardware-in-the-loop on one RaspberryPi. While you can switch from Matlab to Python (and vice versa) in all aspects at any time, this does not apply here. Anyone who has got to know Simulink will definitely not want to be without it again.

Data import and export
Both Matlab and Python offer interfaces to all possible data resources from local CSV files to SQL databases etc.

For those who have worked with Excel up to now and perhaps would like to continue to keep their data in Excel, a simple function extension of Excel may be a decisive advantage. Matlab offers an Excel interface as a direct plug-in for Excel. Of course there are also functions for Python to read and save Excel files, but you have to do this from Python.

performance

In my post on the nonparametric significance test, Matlab proved to be twice as fast as Python. In the case of string processing or other operations, however, this can also be the other way around. Without doing an extensive performance test, one can at least say that the Matlab standard routines are regularly updated, e.g. when Intel updates its libraries to address the processor. The user usually does not notice this. However, graphics card support came quite late and the Intel Phi boards are not supported to this day. And both are ultimately (in addition to FPGAs) the options for scaling your algorithms.

For a flexible parallelization on several cores, both programs are not particularly useful. The parallelization of loops is still much easier in Matlab (with the corresponding toolbox) than in Python. In principle, you set up the parallelization setting locally and then just write “parfor” instead of “for”. However, in practice it is not that easy. But parallelization on several cores is also something that does not correspond to any entry level in numerics.

For a long time it was a huge advantage for numerical computation to replace loops with array operations. You can do that in both Matlab and Python. Since Matlab and Python treated loop contents as iterable objects, this clear performance advantage of code vectorization is often no longer available.

Extended benefits
Often you want to make your scientific calculations available to others without them having to understand, read or compile the source code, i.e. without them having to have a Python distribution or a Matlab version installed. This question does not arise in Python, because Python is designed in such a way that the code can actually be executed anywhere without installing a development environment using a Python interpreter, i.e. basically runs on the USB stick to the web server. Independent programs can be compiled from Matlab (once again only via a separate toolbox), but the user must also install a file that is several hundred megabytes in size and which is suitable for the Matlab version.

Via the app designer, Matlab offers a very convenient solution for creating interactive applications in a window, similar to what VBA, for example, offers. In Python and other languages, of course, much more and more flexible things are possible here, but that also requires significantly more programming knowledge. The creation of an interactive window may require more training than the actual scientific programming. The Matlab app designer strikes a good middle ground. You can even comfortably work together several programs using variables in the workspace without having to deal with interface programming between different applications. This is where Matlab's uniform in-the-box approach pays off. The applications in Matlab are not necessarily beautiful and individual, but as is well known, good design does not improve the scientific result.

If you want to roll out your designed algorithms on a professional scale later, you will find options for C transfer in both Matlab and Python. Here, however, we are no longer talking about an entry into scientific programming, but of an advanced application. Basically, in such a progress, there is nothing against continuing to use both Python and Matlab. If you have already familiarized yourself with one of the two, switching to the other language is also not difficult.
In Matlab there is both a direct Python interface and a Matlab interface for Python. In addition, files saved in Matlab can be opened and saved in Python. The programs can therefore work hand in hand.

Speaking of interfaces. A big advantage of Python compared to Matlab is that many scientific special programs (for example the FEM software Abaqus, but also the DataScience program RapidMiner) now use Python to individually expand, remotely control and automate the functionalities of the respective programs. If you want to calculate more than just scientific equations numerically, Python is probably the best and most flexible way to get started with programming in all directions. Large universities such as MIT or Harvard are now using Python as an introduction to programming, and not without reason.

Python is on the advance in many areas and interfaces and has a growing community. In contrast, the Matlab community has been active for a long time and is perhaps more broadly positioned in the sciences in which numerical calculations are carried out. But even if Matlab currently covers a large number of scientific disciplines, the question arises whether Matlab can keep up with the topicality of Python extensions and OpenSource approaches in the long term. For particularly hyped topics with economic interest, such as currently data science, Matlab is currently showing no weakness.

Summary
Anyone who has a general interest in programming is generally well advised to get started with Python and is therefore well prepared for a wide variety of applications, also outside of numerical calculations. Those who rely purely on scientific or engineering calculations, on the other hand, will find both an easier entry and a better in-the-box experience in Matlab - of course only if the cost aspect of Matlab does not represent an exclusion criterion. In principle, the calculations can be carried out with both programs. In addition to comfort aspects and a few technical details, the main decision criterion between the two programs is the Simulink functionality, which is only available in Matlab.

Matlab or Python for getting started with numerical computing?
5 (100%) 7 votes