Joe
Joe

Reputation: 1045

What is .' appended to end of list for use in bsxfun do?

From the Matlab docs for bsxfun an example is given:

fun = @(A,B) A.*sin(B);
A = 1:7;
B = pi*[0 1/4 1/3 1/2 2/3 3/4 1].'; % what is the .' at the end?
C = bsxfun(fun,A,B)

I understand how to use bsxfun, but I don't understand what .' does? I understand ' to be transpose, but what is .'?

Upvotes: 2

Views: 107

Answers (2)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38042

Did you know you can type things like help / or help .? You'll get the help for all MATLAB operators and special characters.

In order to get the help on transpose like that, you first have to know a bit about MATLAB syntax. It is a bit odd in that the apostrophe serves a triple role:

  • to denote strings (quote)
  • to escape itself
  • for transposing vectors/matrices

The second one allows you to write an literal apostrophe inside a string:

>> a = 'This is how it''s done!';

This triple role is due to historic reasons, and in my opinion rather unfortunate. Because, if you try

>> help .'

MATLAB syntax rules dictate that this will be interpreted as

>> help('.'')

which is an unterminated string. Therefore, to get the help on the transposes, you'll have to "escape" the apostrophe:

>> help .''

Which gives you the answer:

...
transpose  - Transpose                         .'
ctranspose - Complex conjugate transpose        ' 
...

so the difference is this:

>> A = [1+1i  2+2i
        3+3i  4+4i];
>> A'
ans =
   1.0000 - 1.0000i   3.0000 - 3.0000i
   2.0000 - 2.0000i   4.0000 - 4.0000i

>> A.'
ans =
   1.0000 + 1.0000i   3.0000 + 3.0000i
   2.0000 + 2.0000i   4.0000 + 4.0000i

Here's some fun you can have with that:

>> A' * A.'

ans =
    14    30
    20    44

Of course, you could have also gotten the help with help transpose or help ctranspose, because the apostrophe-notations are just aliases for those longer function names. Those long function names are also the ones to use when you are going into OOP and operator overloading:

classdef MyAwesomeClass

    methods

        %// this is how you'd overload the transpose operator for your class:
        function obj = transpose(obj)
            %// implement transpose here
        end

    end

end

Upvotes: 4

P0W
P0W

Reputation: 47824

From here

b = a.' computes the non-conjugate transpose of matrix a and returns the result in b.

See difference with following example :

>> [1+6i 7-3i 5]'

ans =

   1.0000 - 6.0000i
   7.0000 + 3.0000i
   5.0000          

>> [1+6i 7-3i 5].'

ans =

   1.0000 + 6.0000i
   7.0000 - 3.0000i
   5.0000

In your case you've no complex numbers, so the result of two (with or without dot .) will be the same

Upvotes: 4

Related Questions