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

26 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 5 de Jul. de 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.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by