Search code examples
matlabcomplex-numbers

matlab switch complex and real parts of a number


I have a column list of complex numbers (about 200k long). I want to switch all the real and imaginary parts. I'm pretty sure there is a single multiplication I can do to accomplish this, but I can't find a formula online. This is the best way I've found so far but it's too slow for my needs (it needs to run realtime):

>> vec = [complex(1,11);complex(2,22);complex(3,33)]

vec =

   1.0000 +11.0000i
   2.0000 +22.0000i
   3.0000 +33.0000i

>> complex(imag(vec),real(vec))

ans =

  11.0000 + 1.0000i
  22.0000 + 2.0000i
  33.0000 + 3.0000i

Solution

  • I'm not sure if there is a built-in operation for this, but I do see a speed increase by not using complex function:

    >> imag(vec) + real(vec)*1i
    ans =
      11.0000 + 1.0000i
      22.0000 + 2.0000i
      33.0000 + 3.0000i
    

    and also this way

    >> conj(vec)*1i
    ans =
      11.0000 + 1.0000i
      22.0000 + 2.0000i
      33.0000 + 3.0000i
    

    which I think looks a lot cleaner.