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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thomas
  • 6,515
  • 1
  • 31
  • 47
  • 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. – Mikhail Poda Oct 05 '10 at 07:02
  • 2
    Agree with @Mikhail. Everything but the basic matrix operations seems like a poorly-designed hack. – Seth Johnson Mar 07 '11 at 02: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. – Cris Luengo Dec 12 '22 at 20:42

6 Answers6

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.

Community
  • 1
  • 1
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • 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. – Thomas Oct 04 '10 at 02:01
  • @Thomasballinger: I edited in a link to a somewhat related question about Matlab for programmers. – Jonas Oct 04 '10 at 02:03
  • 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! – Thomas Oct 16 '10 at 14:55
  • Good links, and good points made in the mentioned tutorial - I'll post a few more of them at this question sometime soon. – Thomas Nov 18 '10 at 04:29
  • Matrices, Indexing, etc is quite similar IMO. What I have difficulties with is the other types like `set`s, `dict`s and - especially - `list`s (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. – embert Sep 03 '14 at 06:07
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.

ars
  • 120,335
  • 23
  • 147
  • 134
  • These "translations" are very nice because they can work both ways, but a discussion of how Matlab works would be ideal. – Thomas Oct 04 '10 at 13:47
  • 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. – ars Oct 05 '10 at 01:24
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steve Tjoa
  • 59,122
  • 18
  • 90
  • 101
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
k107
  • 15,882
  • 11
  • 61
  • 59
  • 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. – horchler Mar 07 '14 at 18:13
  • Yes, avoid `for` loops. Unfortunately the MATLAB documentation is terse on this point. – Peter Mortensen Jul 29 '14 at 14:29
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.

Thomas
  • 6,515
  • 1
  • 31
  • 47
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.)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Thomas
  • 6,515
  • 1
  • 31
  • 47
  • ballinger: I can email you the (short) tutorial I made eventually, if you're interested. – Jonas Oct 04 '10 at 16:20
  • 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. – Thomas Oct 04 '10 at 19:49