What is MATLAB good for? Why is it so used by universities? When is it better than Python?


I've been recently asked to learn some MATLAB basics for a class.

What does make it so cool for researchers and people that works in university? I saw it's cool to work with matrices and plotting things... (things that can be done easily in Python using some libraries).

Writing a function or parsing a file is just painful. I'm still at the start, what am I missing?

In the "real" world, what should I think to use it for? When should it can do better than Python? For better I mean: easy way to write something performing.

UPDATE 1: One of the things I'd like to know the most is "Am I missing something?" :D

UPDATE 2: Thank you for your answers. My question is not about buy or not to buy MATLAB. The university has the possibility to give me a copy of an old version of MATLAB (MATLAB 5 I guess) for free, without breaking the license. I'm interested in its capabilities and if it deserves a deeper study (I won't need anything more than basic MATLAB in oder to pass the exam :P ) it will really be better than Python for a specific kind of task in the real world.

10/13/2009 6:24:39 PM

Accepted Answer

Adam is only partially right. Many, if not most, mathematicians will never touch it. If there is a computer tool used at all, it's going to be something like Mathematica or Maple. Engineering departments, on the other hand, often rely on it and there are definitely useful things for some applied mathematicians. It's also used heavily in industry in some areas.

Something you have to realize about MATLAB is that it started off as a wrapper on Fortran libraries for linear algebra. For a long time, it had an attitude that "all the world is an array of doubles (floats)". As a language, it has grown very organically, and there are some flaws that are very much baked in, if you look at it just as a programming language.

However, if you look at it as an environment for doing certain types of research in, it has some real strengths. It's about as good as it gets for doing floating point linear algebra. The notation is simple and powerful, the implementation fast and trusted. It is very good at generating plots and other interactive tasks. There are a large number of `toolboxes' with good code for particular tasks, that are affordable. There is a large community of users that share numerical codes (Python + NumPy has nothing in the same league, at least yet)

Python, warts and all, is a much better programming language (as are many others). However, it's a decade or so behind in terms of the tools.

The key point is that the majority of people who use MATLAB are not programmers really, and don't want to be.

It's a lousy choice for a general programming language; it's quirky, slow for many tasks (you need to vectorize things to get efficient codes), and not easy to integrate with the outside world. On the other hand, for the things it is good at, it is very very good. Very few things compare. There's a company with reasonable support and who knows how many man-years put into it. This can matter in industry.

Strictly looking at your Python vs. MATLAB comparison, they are mostly different tools for different jobs. In the areas where they do overlap a bit, it's hard to say what the better route to go is (depends a lot on what you're trying to do). But mostly Python isn't all that good at MATLAB's core strengths, and vice versa.

6/23/2012 2:39:56 AM

Most of answers do not get the point.

There is ONE reason matlab is so good and so widely used:


I am a computer vision phD student and have been using matlab for 4 years, before my phD I was using different languages including C++, java, php, python... Most of the computer vision researchers are using exclusively matlab.

1) Researchers need fast prototyping

In research environment, we have (hopefully) often new ideas, and we want to test them really quick to see if it's worth keeping on in that direction. And most often only a tiny sub-part of what we code will be useful.

Matlab is often slower at execution time, but we don't care much. Because we don't know in advance what method is going to be successful, we have to try many things, so our bottle neck is programming time, because our code will most often run a few times to get the results to publish, and that's all.

So let's see how matlab can help.

2) Everything I need is already there

Matlab has really a lot of functions that I need, so that I don't have to reinvent them all the time:

change the index of a matrix to 2d coordinate: ind2sub extract all patches of an image: im2col; compute a histogram of an image: hist(Im(:)); find the unique elements in a list unique(list); add a vector to all vectors of a matrix bsxfun(@plus,M,V); convolution on n-dimensional arrays convn(A); calculate the computation time of a sub part of the code: tic; %%code; toc; graphical interface to crop an image: imcrop(im);

The list could be very long... And they are very easy to find by using the help.

The closest to that is python...But It's just a pain in python, I have to go to google each time to look for the name of the function I need, and then I need to add packages, and the packages are not compatible one with another, the format of the matrix change, the convolution function only handle doubles but does not make an error when I give it char, just give a wrong output... no

3) IDE

An example: I launch a script. It produces an error because of a matrix. I can still execute code with the command line. I visualize it doing: imagesc(matrix). I see that the last line of the matrix is weird. I fix the bug. All variables are still set. I select the remaining of the code, press F9 to execute the selection, and everything goes on. Debuging becomes fast, thanks to that.

Matlab underlines some of my errors before execution. So I can quickly see the problems. It proposes some way to make my code faster.

There is an awesome profiler included in the IDE. KCahcegrind is such a pain to use compared to that.

python's IDEs are awefull. python without ipython is not usable. I never manage to debug, using ipython.

+autocompletion, help for function arguments,...

4) Concise code

To normalize all the columns of a matrix ( which I need all the time), I do: bsxfun(@times,A,1./sqrt(sum(A.^2)))

To remove from a matrix all colums with small sum:


To do the computation on the GPU:

gpuX = gpuarray(X); 
%%% code normally and everything is done on GPU

To paralize my code:

parfor n=1:100
%%% code normally and everything is multi-threaded

What language can beat that?

And of course, I rarely need to make loops, everything is included in functions, which make the code way easier to read, and no headache with indices. So I can focus, on what I want to program, not how to program it.

5) Plotting tools

Matlab is famous for its plotting tools. They are very helpful.

Python's plotting tools have much less features. But there is one thing super annoying. You can plot figures only once per script??? if I have along script I cannot display stuffs at each step ---> useless.

6) Documentation

Everything is very quick to access, everything is crystal clear, function names are well chosen. With python, I always need to google stuff, look in forums or stackoverflow.... complete time hog.

PS: Finally, what I hate with matlab: its price

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow