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

27 vues (au cours des 30 derniers jours)
MathWorks Support Team
MathWorks Support Team le 5 Juil 2012
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.

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 5 Juil 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.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by