Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Loren on the Art of MATLAB

February 21st, 2007

Beginner Woes

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

11 Responses to “Beginner Woes”

  1. mark replied on :

    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 “==”

  2. KEN dAVIS replied on :

    Another common problem is confusing parentheses and braces when indexing into a cell array.

  3. vish replied on :

    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.

  4. Matthew Simoneau replied on :

    My favorite is command/function duality, that is “sin(a)” vs. “sin a”. Explaining the difference is one of my favorite interview questions.

  5. Jos vdG replied on :

    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

  6. Arne Jenssen replied on :

    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.

  7. Tim Davis replied on :

    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” …

  8. Joel MacAuslan replied on :

    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.

  9. Loren replied on :

    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

  10. Florian Brucker replied on :

    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.

  11. Joel MacAuslan replied on :

    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.

Leave a Reply


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • Ulla Vainio: That error bar width adjustment was extremely useful and I would never have figured it out myself....
  • Peter Perkins: Jessee, there is a property that you can use to tag variables with units. For example, >> load...
  • Jessee: I could potentially see myself using dataset for casually looking at data, but from an application standpoint...
  • Loren: Oktay- It very much depends on the details of the calculations you are doing. Vectorization can sometimes...
  • Oktay: Hello, Is there any significant difference between using: - Vectorization inside a subfunction - Benefiting...
  • Loren: Clare- Yes, sum can sum a double vector: x = [.3 .4 pi/3] y = sum(x) x = 0.3 0.4 1.0472 y = 1.7472 You must...
  • Clare J: R2007a - Student Version When I use sum to sum a vector of type double I get this error message: ???...
  • Sarah Zaranek: Hi Jacob, Sorry about the slow response. You are correct that the code would be slower without the...
  • Navaneethan Santhanam: Thanks a lot, Loren! That worked perfectly.
  • Mike N: Should it be OK to use “persistent 221; variables in a deployed application? What if I have two...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics