16

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.?

3
  • 3
    Thnk about MATLAB as an emulation of a pocket calculator on PC, the language features are from 70' and are very basic. Newer features like OO is modelled after python but is not really usable because of performance. Commented Oct 5, 2010 at 7:02
  • 2
    Agree with @Mikhail. Everything but the basic matrix operations seems like a poorly-designed hack. Commented Mar 7, 2011 at 2:06
  • I don't know what kind of pocket calculator @Mikhail has, but it's a ridiculous comparison. Like calling a 4 master a dinghy. I don't think MATLAB's OO is modeled after Python, class definitions are very, very different. What is true is that NumPy and Matplotlib and other Python packages were originally modelled after MATLAB. Commented Dec 12, 2022 at 20:42

6 Answers 6

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The great help and other IDE features like the debugger are my favorite things about using Matlab. It's great that if I need to figure out how to something that I assume there's a built in function for, it's pretty easy to find.
@Thomasballinger: I edited in a link to a somewhat related question about Matlab for programmers.
I've finally added contact info to my profile, so whenever the tutorial is in a state you wouldn't mind sharing, let me know. Thanks!
Good links, and good points made in the mentioned tutorial - I'll post a few more of them at this question sometime soon.
Matrices, Indexing, etc is quite similar IMO. What I have difficulties with is the other types like sets, dicts and - especially - lists (and maybe objects in general). In python anything can be put in a list, one can loop it and simply convert it to matrices. In MATLAB one probably uses a cell, but it works quite differently somehow. So a "how to use cell's for pythonists" would be really helpful.
6

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.

2 Comments

These "translations" are very nice because they can work both ways, but a discussion of how Matlab works would be ideal.
I see what you're getting at, unfortunately don't know of a particular book or tutorial, but updated the answer with some links you might find useful.
3
  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.

Comments

2

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.

2 Comments

3. Don't use blanket statements. All languages have their limitations. Both classes and for loops have their place and in many particular applications are fine, if not superior according to many criteria. As with anything, the key is learning when and how to apply these to a particular problem. Simply saying "don't" doesn't make for a nuanced helpful answer.
Yes, avoid for loops. Unfortunately the MATLAB documentation is terse on this point.
1

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.

Comments

0

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.)

2 Comments

ballinger: I can email you the (short) tutorial I made eventually, if you're interested.
That'd be great, thanks. If it's alright, I'll post some of the pointers that are most relevant for python to matlab here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.