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

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

  12. Antony Drew replied on :

    Hi Loren,

    I have some data in a 3D matrix. Specifically, I have 100 layers of a [90 column by 100 row] rectangle. When I click inside the Array Editor, it always “wraps” the columns onto subsequent rows instead of preseting the data in neat grids which I could then paste into Excel. Also, when I try to use the code: fprintf(fid,’%7.4f %7.4f %7.4f %7.4f\n’,PortWts), the data comes out in various columns and not awlays the number of ones I specified with %f. To sum up, what is the best way to export a complex matrix or array in order to preserve the neatness of grids so that I don’t have to copy and paste and fix them in EXCEL? Even the ‘diary’ function exports the data in a wrapped column format. I basically want the data to appear as nicely as it does in the Array Editor for a simple 2-D matrix or rectangle, but for subsequent layers of more complex data etc. I use the student version by the way. Please advise.

    Tony

  13. Loren replied on :

    Antony-

    You say “the data comes out in various columns and not awlays the number of ones I specified with %f”. That’s because the fprintf command is vectorized and repeats the format if the data you present are larger. You need to make sure the format is an exact multiple of the number of elements/row (and that that is a fixed number) for your data.

    –Loren

  14. Gunay replied on :

    Hi,
    I have a matrix where each column is a set of data. I would like multiple each column element by element with a vector. Is the following the most efficient way?

    myMatrix = magic(3)
    myVector = [1 2 3]'
    repVector = repmat(b,1,3)
    myOutput = myMatrix.*repVector
    
  15. Loren replied on :

    Gunay-

    It will probably depend on how big b is (and what it is – your code doesn’t run as you wrote it above). If your replicated vector makes too large a matrix, you might look into whether a sparse diagonal matrix will work better for you. And perhaps also bsxfun, but I can’t think of how that would work exactly, esp. since I don’t have MATLAB nearby.

    –loren


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

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