There are a few pitfalls that new users of MATLAB almost always fall into, depending on their applications. I thought it might be nice to summarize a few of them here, and, I hope, use you the readers to add to the list.
Contents
Matrix Multiplication vs. Elementwise
From its inception, MATLAB was meant to be a MATrix LABoratory. Since initially the only datatype was a two-dimensional array, Cleve chose * for the matrix multiplication operator. To multiply two arrays elementwise, use .* instead.
A = magic(3) B = diag([-1 1 -1]) AB = A*B eachAB = A.*B
A =
8 1 6
3 5 7
4 9 2
B =
-1 0 0
0 1 0
0 0 -1
AB =
-8 1 -6
-3 5 -7
-4 9 -2
eachAB =
-8 0 0
0 5 0
0 0 -2
Sometimes new users type A*B and find it takes a really long time because they are expecting the elementwise output. For a square matrix of order N, and without accounting for any special characteristics (e.g., if one of the matrices is diagonal, sparse, banded, etc.), then matrix multiplication takes
operations whereas .* takes
Transpose for Complex Data
Again, going back to MATLAB's roots, the original basic datatype was not only two-dimensional, but the values were double and complex (with appropriate storage optimizations if the data were real-valued). Being the MATrix LABoratory that MATLAB is, we needed an operator for transposing a matrix and chose '. Looking at the documentation for ', we see that ' performs the complex conjugate transpose. We also see that .' performs the transpose without doing the conjugation. For real matrices, ' and .' are the same. But for complex matrices, they differ.
Ai = A+i*[1 2 3; 4 5 6; 7 8 9] Aict = Ai' Ait = Ai.'
Ai = 8.0000 + 1.0000i 1.0000 + 2.0000i 6.0000 + 3.0000i 3.0000 + 4.0000i 5.0000 + 5.0000i 7.0000 + 6.0000i 4.0000 + 7.0000i 9.0000 + 8.0000i 2.0000 + 9.0000i Aict = 8.0000 - 1.0000i 3.0000 - 4.0000i 4.0000 - 7.0000i 1.0000 - 2.0000i 5.0000 - 5.0000i 9.0000 - 8.0000i 6.0000 - 3.0000i 7.0000 - 6.0000i 2.0000 - 9.0000i Ait = 8.0000 + 1.0000i 3.0000 + 4.0000i 4.0000 + 7.0000i 1.0000 + 2.0000i 5.0000 + 5.0000i 9.0000 + 8.0000i 6.0000 + 3.0000i 7.0000 + 6.0000i 2.0000 + 9.0000i
Why does this trip people? Sometimes when I write a general algorithm, I expect it to work regardless of the data, be it real or complex. Now suppose my code is working with column vectors, but I am willing, as a convenience, to have the code handle rows as well. I just need to transform them, in this case transpose. If I am not careful here, and use ', I will conjugate the original data and I didn't mean to.
Other Beginner Woes?
I have a few more topics in the Woes category that I will cover in a later blog. Some of them happen to new users, and some happen to even those of us with more MATLAB experience. Please post other common pitfalls you've seen new users experience here.
Get
the MATLAB code
Published with MATLAB® 7.4

I think the indexing convention may be confusing to new users. Also, I’d imagine a lot of people write “=”, for instance in an if or while statement when they should write “==”
Another common problem is confusing parentheses and braces when indexing into a cell array.
i disagree with #1 and #2 above,
these mistakes, though common are not a problem since they do not compute. and the error matlab spits out is very explicit.
My favorite is command/function duality, that is “sin(a)” vs. “sin a”. Explaining the difference is one of my favorite interview questions.
One of my favourite pitfalls involves true vs one. As an example, for which even MatLab “fails”:
X = 10:10:50 ;
i = true(1,5)
A = X(i)
j = ones(1,5)
B = X(j)
% and of course(!?) …
isequal(A,B)
% … but (!) …
isequal(i,j)
Jos
I think that using i and j as a variable is a very bad habit commonly seen. This will make a mess when using complex values.
I agree with Arne that the i and j confusion can be a “woe”, but I would like to kindly disagree with his solution. These variables are very commonly used as indices in algorithms. The best solution to the “beginner woe” of redefining i and j is to not use i and j as complex units at all. The constant “1i” should always be used instead. A more common beginner woe is to abuse MATLAB’s expressive power, when performance is an issue. For example, allowing an array to grow arbitrarily, just by assigning a value to it, is very handy and sometimes a very useful solution to a problem. It shouldn’t be used if the array can be easily preallocated. The same problem occurs when abusing sub-matrix assignment for sparse matrices (A(i,j)= … when A is sparse).
Another problem I see is just the opposite … when MATLAB’s expressiveness is *not* used, resulting in ugly code. Some beginners try to write MATLAB as if it’s C/Fortran/Java. They write ugly and lengthy loops when a simple, elegant, and easy-to-read one-line vector statement could be used instead.
My favorite abuse of MATLAB, however, is using x=inv(A)*b instead of x=A\b. I would love it if MATLAB were to generate a warning message whenever it saw “inv(A)*b” …
I think the problem with Complex conjugate-transpose is more subtle than a mere “beginner’s gotcha”. Specifically, the dot-operator notation in ALL other cases means “like the operator, but applied element-by-element”. That is NOT what “.’” means. By analogy with the other dot-operators, it should mean “conjugate of each element”.
It is one thing to trip up beginners who have been “damaged” [Dijkstra’s word] by other languages and therefore do not use the Matlab language appropriately. It is quite another to introduce an INTERNAL inconsistency into that language.
Joel-
I understand what you are saying about dot meaning element-wise for the other operations, but what does element-wise transpose mean? Transposing a scalar is the same scalar. I don’t see why .’ should then mean conjugate each element; that’s what the function conj does. So it’s not clear to me that defining .’ the way you suggest would have been useful and not confusing.
–Loren
Took me some time to notice that log ~= log10. I somehow expected that the natural logarithm would be called ln.
About “.’”: I think what Joel says does make sense. This “like the operator, but applied element-by-element” is how many people (including me) see the period operator. So if ‘ is conjugate transpose, .’ would conjugate the elements. Perhaps using ‘ for transpose only and ” for conjugate transpose would’ve been clearer, but that’s a question of personal taste IMO.
Loren,
I do not argue whether .’ is a good notation for conjugating every element. I only observe that MATLAB has adopted exactly that interpretation for the other dot-operators, without exception.
And BTW, the fact that it would trivialize part of the operation — the transpose but not the conjugation — is harmless. (You could make an even stronger case for the uselessness of dot-plus. But it is harmless as an operator in its own right, and its semantics are consistent with what dot does to the other common operators. So putting it in the language is probably a GOOD design choice, not merely a harmless one.)
Perhaps conjugate-transpose should not have been given the simple apostrophe notation. But once it was, the meaning of .’ was really not open for negotiation. The adopted usage introduced a completely avoidable inconsistency in the language, and was thus a mistake in language design.
FWIW, this is (IMO) not the worst of MATLAB’s inconsistencies — and yet MATLAB is far, far better than most common languages in its design.