If you want to make sure that the complex
data type is maintained, explicitly cast the number to complex
. Therefore:
y(1) = complex(x(1));
Because x(1)
only has a real component, MATLAB automatically converts this to real
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 to complex
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 all real
as there are no imaginary components. If y
had at least one value that was complex valued, y
would still be maintained as complex
. Take a look at this code:
x = complex(1:2,0:1);
y = complex(zeros(1,2), [3 5]);
whos x y
Name Size Bytes Class Attributes
x 1x2 32 double complex
y 1x2 32 double complex
Now, let's try that assignment and examining the classes of x
and y
:
y(1) = x(1);
whos x y
Name Size Bytes Class Attributes
x 1x2 32 double complex
y 1x2 32 double complex
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 to complex
. Try, for example:
x = 1:5;
whos x
Name Size Bytes Class Attributes
x 1x5 40 double
Now using the same x
array, try:
x(3) = 1 + 4i;
whos x
Name Size Bytes Class Attributes
x 1x5 80 double complex
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 that y
is still a complex valued array. As such:
x = complex(1:2,0:1);
y = complex(zeros(1,2),0);
y(1) = x(1) + i*eps;
who x y
Name Size Bytes Class Attributes
x 1x2 32 double complex
y 1x2 32 double complex
eps
stands for machine epsilon. If you display y
and show more significant digits, this is what we see:
format long
y
y =
1.000000000000000 + 0.000000000000000i 0.000000000000000 + 0.000000000000000i
Try that and see if that works for you.