How Do I Prevent MATLAB from Dropping the "complex" Attribute of an Array? -


the following matlab code snippet, creates 2 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 2 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 1 prevent complex attribute being dropped?

for record, octave same.

in practice, x function argument first entry happens have 0 imaginary part, , y return value being preallocated.

if want make sure complex data type maintained, explicitly cast number complex. therefore:

y(1) = complex(x(1)); 

because x(1) has real component, matlab automatically converts real in order save space. can see, more efficient store real component if complex number purely real it's 8 bytes number in comparison complex it's 16 bytes number - 8 real component , 8 imaginary component.

also in code,y technically real there no imaginary components. if y had @ least 1 value complex valued, y still maintained complex. take @ 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 assignment , examining classes of x , y:

y(1) = x(1); whos x y  name      size            bytes  class     attributes  x         1x2                32  double    complex    y         1x2                32  double    complex  

also, sidenote, shouldn't concerned x being converted purely real. place @ least 1 complex valued number array, x automatically gets promoted complex. try, example:

x = 1:5; whos x  name      size            bytes  class     attributes  x         1x5                40  double   

now using same x array, try:

x(3) = 1 + 4i; whos x  name      size            bytes  class     attributes  x         1x5                80  double    complex  

edit

going comments, can make sure array stay complex add infinitesimal number imaginary part of x(1). number small enough numerical differences virtually zero, enough respect y still complex valued array. such:

x = complex(1:2,0:1); y = complex(zeros(1,2),0); y(1) = x(1) + i*eps; x y  name      size            bytes  class     attributes  x         1x2                32  double    complex    y         1x2                32  double    complex   

eps stands machine epsilon. if display y , show more significant digits, see:

format long y  y =    1.000000000000000 + 0.000000000000000i  0.000000000000000 + 0.000000000000000i 

try , see if works you.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -