MATLAB: How to pre-allocate memory for a complex matrix

allocateimaginaryMATLABmemoryprevector

I want to pre-allocate memory for a complex variable because the MATLAB documentation states that one should pre-allocate memory to speed up performance, i.e.
x = zeros(1000);
However, this only allocates the real-part. How do I pre-allocate a complex number? i.e.
x = zeros(1000) + j*zeros(1000);
This does not work. Instead it only allocates memory for the real part of the complex number array.

Best Answer

When MATLAB assigns a complex array, it scans the array to determine if the imaginary part is all zero. If this is the case, MATLAB removes the imaginary portion of the array to reserve memory.
Thus
x = zeros(1000) + j*zeros(1000);
is equivalent to:
x = zeros(1000);
To work around this issue, execute the following command:
x = complex(zeros(1000));
This will create a complex vector of the appropriate size.
Related Question