The following MATLAB code snippet, which creates two arrays of complex numbers,
x = complex(1:2,0:1);
y = complex(zeros(1,2),0);
whos x y
prints
Name Size Bytes Class Attributes
x 1x2 32 double complex
y 1x2 32 double complex
as expected. However, after these two additional statements,
y(1) = x(1);
whos x y
the following gets printed:
Name Size Bytes Class Attributes
x 1x2 32 double complex
y 1x2 16 double
How can one prevent the complex attribute from being dropped?
For the record, Octave does the same.
In practice, x
is a function argument whose first entry happens to have a zero imaginary part, and y
is the return value that is being preallocated.
I think I found what I was looking for in this answer. In sum, an array of complex numbers has two memory blocks for the data: one to store the real parts and one to store the imaginary parts. The best course of action should be to initialize
y
to an array of doubles of the right size and let MATLAB add the second memory block on demand. The number of memory allocations is at most two, and there seems to be no way to reduce it.If you want to make sure that the
complex
data type is maintained, explicitly cast the number tocomplex
. Therefore:Because
x(1)
only has a real component, MATLAB automatically converts this toreal
in order to save space. As you can see, it would be more efficient to simply store the real component if the complex number is purely real as it's 8 bytes a number in comparison tocomplex
where it's 16 bytes a number - 8 for the real component and 8 for the imaginary component.Also in your code,
y
would technically be allreal
as there are no imaginary components. Ify
had at least one value that was complex valued,y
would still be maintained ascomplex
. Take a look at this code:Now, let's try that assignment and examining the classes of
x
andy
:Also, as a sidenote, you shouldn't be concerned with
x
being converted to purely real. As soon as you place at least one complex valued number into this array,x
automatically gets promoted tocomplex
. Try, for example:Now using the same
x
array, try:Edit
Going with your comments, what you can do to make sure that the array will stay complex would be to add an infinitesimal number to the imaginary part of
x(1)
. A number small enough so that numerical differences are virtually zero, but enough to respect thaty
is still a complex valued array. As such:eps
stands for machine epsilon. If you displayy
and show more significant digits, this is what we see:Try that and see if that works for you.