26

When transposing vectors/matrices in MATLAB, I've seen and used just the ' (apostropohe) operator for a long time.

For example:

>> v = [ 1 2 3 ]'

v = 

    1
    2
    3

However this is the conjugate transpose as I've recently found out, or ctranspose.

This seems to only matter when there are complex numbers involved, where if you want to transpose a matrix without getting the conjugate, you need to use the .' opertator.

Is it good practice to also use the .' for real matrices and vectors then? What should we be teaching MATLAB beginners?

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
legas
  • 408
  • 4
  • 10
  • Good question :)... which has generated a lot of discussion. Nice seeing you here! Haven't seen you around before. – rayryeng Aug 06 '14 at 00:24
  • 1
    Hi @rayryeng, thank you! I saw your replies to a few Matlab posts recently! I'm in awe. I've been lurking around, finally have a bit of courage to start contributing. Great dialogue between you, Luis Mendo, and Yvon. – legas Aug 06 '14 at 00:42
  • 1
    Don't be shy :) Start small with questions that you know you can definitely answer. I started with questions that dealt with simple subsetting or indexing into matrices, or anything dealing with `plot`. I got more confident when I started learning about other MATLAB functions that Luis Mendo, Divakar, Amro, natan, etc. put forward and that further strengthened my base to answer more advanced questions. Also, answering questions about MATLAB that deal directly with your field of research / work helps too. I answer a lot of the image processing questions. Hope to see you around more! – rayryeng Aug 06 '14 at 01:13
  • 5
    @legas It's a good idea to contribute. You'll find you learn a lot by answering and seeing other people's answers. My Matlab skills have improved a lot that way. But watch out, it can be _very addictive_ :-) – Luis Mendo Aug 06 '14 at 01:16
  • 3
    @LuisMendo - I definitely agree. I've had a SO account for 6 months, but I didn't start actively answering questions until about 3 months ago. Over 3 months, I gained about 6500 reputation.... answering questions is very addictive! – rayryeng Aug 06 '14 at 01:20
  • 2
    +1 nice question. It's a good practice distinguishing between `'` and `.'` even when you have nothing to do with complex numbers. Much like [avoiding the use of `i` and `j` as variable names](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab). – Shai Aug 06 '14 at 05:57
  • 2
    One thing just forgot to mention. I'm really confused by the `.` Try to follow the pattern - "The `.` in `A.*B` means to perform `*` on _each element_ in A and B; the `.` in `A./B` means to perform `/` on _each element_ in A and B; the `.'` in `A.'` means to perform `'` on _???_ in A ?? How much sense does it make that "element-wise transpose" a matrix? If I _were_ the student how could I establish an analogy between `.*` and `.'`? – Yvon Aug 06 '14 at 08:20
  • 1
    @Yvon Yes, the `.` notation in `.'` is confusing. It doesn't mean "element-wise" in this case – Luis Mendo Aug 06 '14 at 09:53

4 Answers4

28

Interesting question!

I would definitely say it's good practice to use .' when you just want to transpose, even if the numbers are real and thus ' would have the same effect. The mains reasons for this are:

  1. Conceptual clarity: if you need to transpose, just transpose. Don't throw in an unnecessary conjugation. It's bad practice. You'll get used to writing ' to transpose and will fail to notice the difference. One day you will write ' when .' should be used. As probable illustrations of this, see this question or this one.

  2. Future-proofness. If one day in the future you apply your function to complex inputs the behaviour will suddenly change, and you will have a hard time finding the cause. Believe me, I know what I say1.

Of course, if you are using real inputs but a conjugation would make sense for complex inputs, do use '. For example, if you are defining a dot product for real vectors, it may be appropriate to use ', because should you want to use complex inputs in the future, the conjugate transpose would make more sense.

1 In my early Matlab days, it took me quite a while to trace back a certain problem in my code, which turned out to be caused by using ' when I should have used .'. What really got me upset is, it was my professor who had actually said that ' meant transpose! He forgot to mention the conjugate, and hence my error. Lessons I learned: ' is not .'; and professors can tell you things that are plain wrong :-)

Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 1
    Hahahah! I made a reference to you in my post... and look who decides to write one :) +1 from me – rayryeng Aug 06 '14 at 00:12
  • 2
    @rayryeng I couldn't resist answering. You know what strong feelings I have on this :-) – Luis Mendo Aug 06 '14 at 00:17
  • Most certainly. I've started to adopt that practice now :) – rayryeng Aug 06 '14 at 00:17
  • 3
    Thanks you, yes I was also taught `'` and the exact same problem happened to me, took me SO LONG to find it. :) – legas Aug 06 '14 at 00:26
  • 2
    @legas - I remember the first time I encountered this slight (but disastrous) difference. I was implementing the DFT on my own without using any built-in functions. To do this vectorized, this requires you to compute a matrix of **complex** coefficients. To do the inverse, you simply have to transpose the matrix. It took me several hours to figure out why it wasn't working... and that's because I used `'` instead of `.'`... d'oh! Like Luis, it's an experience like this that ingrains the concept of `'` and `.'` to be forever stuck in your head. – rayryeng Aug 06 '14 at 00:41
  • 1
    I'm still fairly new to Stack Overflow etiquette, can I accept everyone's answer? Otherwise I choose this one because @LuisMendo has "Matlab's `'` is not transpose; `.'` is." in his profile blurb haha! – legas Aug 06 '14 at 00:48
  • 1
    @rayryeng Unbelievable! My problem with `'` was exactly that one too! I was getting weird values in my Fourier transforms. Direct and inverse transforms were acting exactly the same, which made no sense. How funny you had the same problem :-) – Luis Mendo Aug 06 '14 at 01:08
  • 1
    @legas - you can only accept 1, but voting our answers up are certainly welcome :). FWIW, Luis's answer should definitely be the one that is accepted. He advocates and puts forward more points than Yvon and myself combined. – rayryeng Aug 06 '14 at 01:10
  • @legas You can accept only one answer, but you can upvote as many as you want, if you think they are useful (you need a minimum rep for that). BTW, if I recall correctly, that blurb in my profile was @Divakar's suggestion, after seeing me rant about `'` vs `.'` over and over :-) – Luis Mendo Aug 06 '14 at 01:11
  • 1
    Sure my suggestion paid off ;) Well now you have 2-3 good questions and bunch of folks behind "this philosophy", so you are not alone anymore on this you know! :) BTW You guys had my +1 for these answers and good discussions in comments. – Divakar Aug 06 '14 at 12:12
12

My very biased view: Most cases I use ' are purely "formal", aka not related to mathematical calculations. Most likely I want to rotate a vector like the index sequence 1:10 by 90 degrees.

I seldomly use ' to matrices since it's ambiguous - the first question you've to answer is why you want to make a transpose?

If the matrix is originally defined in a wrong direction, I would rather define the matrix in the correct one it should be, but not turning it afterwards.

To transpose a matrix for a mathematical calculation, I explicitly use transpose and ctranspose. Because by doing so the code is easier to read (don't have to focus on those tiny dots) and to debug (don't have to care about missing dots). Do the following jobs such as dot product as usual.

Yvon
  • 2,903
  • 1
  • 14
  • 36
  • Very nicely put. I agree with the non-ambiguity. +1 – rayryeng Aug 06 '14 at 00:15
  • 3
    I mostly agree with you here except that I have found that sometimes you will define a matrix correctly, use a built-in function on it, and the built-in function will have mysteriously transposed it. I have started peppering my code with statements such as `vectorOut = reshape(vectorOut,size(vectorIn));` to enforce a consistent matrix shape. – craigim Aug 06 '14 at 00:21
  • @rayryeng I also learned _ambiguity_ and avoiding it in a hard way. But luckily it took me not so long to learn to be _prudent_ when naming variables and choosing functions (before actually keyboarding the code!). – Yvon Aug 06 '14 at 00:25
  • I also agree, I definitely like to make my code as clear as possible. The `'` is just so engrained in learning materials that I've used and found, now that I'm writing material for first year engineers/scientists, I feel I need to set the record straight! :) – legas Aug 06 '14 at 00:33
  • 1
    @legas Will it help if you add an example with complex numbers? – Yvon Aug 06 '14 at 00:35
  • 2
    @Yvon - Whenever I give MATLAB seminars, this is one of the points I always stress, so yes an example is always good. A bonus is to show them that using `'` to compute the magnitude squared of a complex vector. – rayryeng Aug 06 '14 at 00:38
10

This is actually a subject of debate among many MATLAB programmers. Some say that if you know what you're doing, then you can go ahead and use ' if you know that your data is purely real, and to use .' if your data is complex. However, some people (such as Luis Mendo) advocate the fact that you should definitely use .' all the time so that you don't get confused.

This allows for the proper handling of input into functions in case the data that are expected for your inputs into these functions do turn out to be complex. There is a time when complex transposition is required, such as compute the magnitude squared of a complex vector. In fact, Loren Shure in one of her MATLAB digests (I can't remember which one...) stated that this is one of the reasons why the complex transpose was introduced.


My suggestion is that you should use .' always if your goal is to transpose data. If you want to do some complex arithmetic, then use ' and .' depending on what operation / computation you're doing. Obviously, Luis Mendo's good practices have rubbed off on me.

Community
  • 1
  • 1
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 3
    It might be too detailed for a beginner to distinguish `'` and `.'` for his/her first day with Matlab. But once introduced with a real problem, it should be the teacher's duty to stress the student on the differences (poor Luis :( – Yvon Aug 06 '14 at 00:32
-1

There are two cases to distinguish here:

  1. Taking transpose for non-mathematical reasons, like you have a function that is treating the data as arrays, rather than mathematical vectors, and you need your error correcting input to get it in the format that you expect.
  2. Taking transpose as a mathematical operation.

In the latter case, the situation has to dictate which is correct, and probably only one of the two choice is correct in that situation. Most often that will be to take the conjugate transpose, which corresponds to ', but there are cases where you must take the straight transpose and then, of course, you need to use .'.

In the former case, I suggest not using either transpose operator. Instead you should either use reshape or just insist that the input be make correctly and throw an error if it is not. This clearly distinguishes these "computer science" instance from true mathematical instances.

Brick
  • 3,998
  • 8
  • 27
  • 47
  • 2
    For an arbitrary matrix, I don't think you can achieve transpose behaviour with `reshape` (you can for vectors) – Luis Mendo Mar 31 '17 at 00:18