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.
Program an exclusive OR operation with logical operators
Set the array elements whose value is 13 to 0
Fibonacci-Sum of Squares
Matlab Basics II - Free Fall
Find the treasures in MATLAB Central and discover how the community can help you!
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office