How do I pre-allocate memory for a complex matrix?

27 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Jul 2012
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.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!