Using transpose versus ctranspose in MATLAB

前端 未结 4 2040
长发绾君心
长发绾君心 2020-11-22 15:34

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

For example:

>&g         


        
相关标签:
4条回答
  • 2020-11-22 16:01

    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.

    0 讨论(0)
  • 2020-11-22 16:02

    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.

    0 讨论(0)
  • 2020-11-22 16:05

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

    0 讨论(0)
  • 2020-11-22 16:20

    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.

    0 讨论(0)
提交回复
热议问题