MATLAB for Python programmers

Question:

I’ve used MATLAB on and off before, but I need to develop a good understanding of it now, and the language I’m most familiar with is Python. Care to describe a MATLAB language feature, idiom, best practice or philosophy as compared to Python?

There’s a terrific amount of buzz for and resources pertaining to going the opposite direction, the MATLAB to (Python + tools) conversion, but that’s not the way I need to go. Which data structures should I swap in, should I use classes, where might NumPy intuition go wrong, etc.?

Asked By: Thomas

||

Answers:

The documentation is one of the strong points of MATLAB. If you need to get into MATLAB, one of the best places to start is the “Getting Started” section. Some of it will be too basic for you, which is a lot better than if it was too advanced, but it will show you the most important aspects of the language.

One of the things you may watch out for is that MATLAB starts indexing at 1. For other aspects of MATLAB programmers may need to be aware of, you may have a look at the answers to this question.

If you need MATLAB for a specific task, the help provides lots of demos that should put you on the right path.

Answered By: Jonas

I found this SciPy.org page helpful, even though it works better for the other direction and doesn’t directly address many core language features.

Along the same lines:

But none of these really explain the MATLAB language and data structures to me like a good book about the language, in a way that leverages my existing knowledge of Python. (The question Jonas links to does though – check that out.)

Answered By: Thomas

Thesaurus of Mathematical Languages, or MATLAB synonymous commands in Python/NumPy is great for looking up “translations” between common MATLAB tasks and NumPy.

I can’t think of a particular tutorial. But one resource I’ve found really useful for picking up the ins and outs of MATLAB are the blogs:

In particular, Loren on the Art of MATLAB and Steve on Image Processing are two that I’ve learned a great deal from.

Answered By: ars
  1. MATLAB has superb documentation. In a world where everyone complains about how bad documentation X is, I think MATLAB’s documentation contributes significantly to its popularity. Python’s is good, too, but MATLAB’s just feels a bit more immediately accessible. You can tell that Mathworks put some care into it.

  2. In MATLAB, the matrix is fundamental. If you do x = 3 in the workspace, you can then do matrix operations to x (as meaningless as that might be) such as transposition, inverse, eigendecomposition, etc. No casting is necessary. In Python/NumPy, you still need to convert arrays to matrices using scipy.matrix before doing matrix operations.

  3. I am not familiar with any explicit, popular MATLAB philosophy analogous to Python’s zen (i.e., import this). But many characteristics are similar: easy experimentation, fast development time, easy to debug and profile, high level, extensible.

  4. MATLAB does not emphasize object orientation like Python. OO is still possible in MATLAB (e.g., classes are supported), but I don’t know many people who make use of it.

  5. I like to think in the following way: NumPy is like the MATLAB core, SciPy is like the MATLAB toolboxes, Matplotlib lets you plot like MATLAB, and iPython is the MATLAB workspace.

  6. Oh yeah… MATLAB starts indexing with 1, not zero! This is a logical consequence of MATLAB’s fundamental idea that every numeric “thing” is a matrix, and in linear algebra, matrices are often indexed starting with 1.

Answered By: Steve Tjoa

You can’t do indexing on function result directly;

from numpy import *
sin(array(range(10))*pi/10)[3]

It doesn’t work in MATLAB; you need to save the result first:

x = sin(0:pi/10:pi)
x(3)

This is from Jonas’s tutorial.

Answered By: Thomas

A couple of performance issues:

  1. Don’t use classes: MATLAB classes are really really slow.

  2. Don’t use for loops: Learn how to vectorize operations. MATLAB is fast at vectorized functions and exorbitantly slow when doing for loops.

Answered By: k107
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.