Problem 1943. Signed Magnitude
Solution Stats
Problem Comments
-
1 Comment
Really needs a better test suite that doesn't have the correct answer in the same position.
Solution Comments
-
5 Comments
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.
-
1 Comment
Nice!
Problem Recent Solvers124
Suggested Problems
-
Find common elements in matrix rows
2081 Solvers
-
Arrange Vector in descending order
8009 Solvers
-
Getting the indices from a vector
6881 Solvers
-
275 Solvers
-
Is this triangle right-angled?
4443 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!