Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using transpose versus ctranspose in MATLAB

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?

like image 602
legas Avatar asked Aug 05 '14 23:08

legas


People also ask

What does transpose function do in Matlab?

The transpose operation switches the rows and columns in a matrix. It is represented by a single quote(').

What is the Ctranspose Matlab?

The complex conjugate transpose of a matrix interchanges the row and column index for each element, reflecting the elements across the main diagonal. The operation also negates the imaginary part of any complex numbers. For example, if B = A' and A(1,2) is 1+1i , then the element B(2,1) is 1-1i .

What is complex conjugate transpose in Matlab?

The complex conjugate transpose operator, A' , performs a transpose and negates the sign of the imaginary portion of the complex elements in A . A' ans = [ x - y*1i, y - x*1i] [ x + y*1i, y + x*1i]


2 Answers

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 :-)

like image 199
Luis Mendo Avatar answered Oct 17 '22 10:10

Luis Mendo


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.

like image 31
Yvon Avatar answered Oct 17 '22 10:10

Yvon