Using sign(x) with -0.00?
17 views (last 30 days)
Show older comments
Hi, I need to go through a vector and check the sign of an oscillatory function but I have a lot of -0.0000's at the start which is going to mess up what I need to do.
Is there anyway to convert them to 0.000 rather than -0.000?
Thanks
0 Comments
Accepted Answer
Jan
on 31 Jan 2013
Edited: Jan
on 31 Jan 2013
Matlab does not have a -0.0, therefore what you are seeing must be a -0.000000000000001 or similar, which is hidden by the display format. To see more digits type this in the command window:
format long g
If you have good reasons to claim, that these data are pure noise, you can smooth it:
x(abs(x) < 1e-10) = 0.0;
But be sure to note the physical motivation of the threshold value.
5 Comments
Jan
on 31 Jan 2013
Or to explain "best": the "e" notation is used, when it can display more significant digits than the 0.0000xyz notation.
James Tursa
on 31 Jan 2013
Edited: James Tursa
on 31 Jan 2013
To clarify what Jan wrote, IEEE double and single floating point (which MATLAB uses) do in fact have distinct -0.0 bit representations (sign bit set to 1 and all other bits reset to 0) that is different from 0.0 (all bits reset to 0). But by default MATLAB will not display the minus sign '-' for these numbers, so when you see something like -0.0000 printed to the screen it is not really a -0.0 value. Rather it represents a small negative number compared to the other numbers that were printed to the screen at the same time (there simply weren't enough digits printed to show the non-zero digits of the small negative number). To see this type the following at the MATLAB prompt:
format hex
0
-0
0 == -0
typecast(0,'uint64') == typecast(-0,'uint64')
More Answers (1)
See Also
Categories
Find more on Scope Variables and Generate Names in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!