Really needs a better test suite that doesn't have the correct answer in the same position.
Why does this not work?
Because x + 0i and complex(x) are fundamentally different in MATLAB. In MATLAB, x+0i is always real variable given that x is real, i.e., it only requires 8 bytes to store x+0i (for real double x), whereas complex(x) is a complex variable which consumes 16 bytes, with 8 bytes storing the real part (x) and 8 bytes storing the imaginary part (which is 0 in this case). You may verify this by the following snippet: [a,b,c] = deal(1,1+0i,complex(1)); whos a b c
Thanks for your neat and detailed explanation, Peng Liu. I had expected/assumed that "x+0i" would work the same as "complex(x)", because in the description on the MATLAB documentation page of the complex() function, it says "if x is real, then z is x + 0i", without directly mentioning anything about the fundamental difference in how the variable is stored.
@yurenchu, in the bottom of the document of MATLAB , there is a Tip, said "If b contains only zeros, then z is complex and the value of all its imaginary components is 0. In contrast, the addition a + 0i returns a strictly real result."
Thanks, Binbin Qi. It seems that I had somehow missed/overlooked that part when I read that page earlier.
Nice!
711 Solvers
306 Solvers
Determine the length of a string of characters
174 Solvers
Element by element multiplication of two vectors
271 Solvers
449 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!