1

This command

Y1 = repmat(0+0i, 10, 1);

appears to create an array of doubles, while this command

Y2 = repmat(0.0001+0.0001i, 10, 1);

correctly creates an array of complex double.

Is there a way in MatLab to allocate for later use an array of complex doubles with magnitudes of zero or am I required to start with very small magnitudes as I've done above??

LGTrader
  • 2,349
  • 4
  • 23
  • 29
  • From a few days ago: [How Do I Prevent MATLAB from Dropping the "complex" Attribute of an Array?](http://stackoverflow.com/questions/27327951/how-do-i-prevent-matlab-from-dropping-the-complex-attribute-of-an-array). – horchler Dec 13 '14 at 07:55

2 Answers2

6

zeros(10,1,'like',1i)

I found it pretty quickly by looking at the help file for zeros.

Nicu Stiurca
  • 8,747
  • 8
  • 40
  • 48
  • I suspect this is the answer MatLab's language spec would provide. (Is there a language spec? I'm too new to know.) Anyway, thanks! – LGTrader Dec 12 '14 at 23:00
  • For those using older versions of Marlab, I believe this [works for R2013a+](http://www.mathworks.com/help/matlab/release-notes.html). The solution from @jez should work regardless of version. – horchler Dec 13 '14 at 07:44
4

complex(zeros(10,1)) seems to do the trick for me. The problem is that your literal 0i actually evaluates to real 0, whereas complex(0) would be the way to get a scalar with explicit zero real and imaginary components.

jez
  • 14,867
  • 5
  • 37
  • 64
  • 1
    Unless the interpreter is clever, this will probably initialize a matrix of reals, then convert to doubles, which might be rather wasteful if OPs actual data is of considerable size. – Nicu Stiurca Dec 12 '14 at 21:04
  • Good point. And this is Matlab we're talking about, so I seriously doubt that the interpreter is that clever. – jez Dec 12 '14 at 22:54
  • Yes, the 'Tips' section of complex sort of suggests the problem I ran into. That said complex(0) or complex(0,0) does seem to work. Thanks! – LGTrader Dec 12 '14 at 23:14