Using sign(x) with -0.00?

16 views (last 30 days)
Martin
Martin on 31 Jan 2013
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

Accepted Answer

Jan
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
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
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')

Sign in to comment.

More Answers (1)

José-Luis
José-Luis on 31 Jan 2013
your_mat(abs(your_mat) < 100*eps) = 0; %or some other limit
  1 Comment
Martin
Martin on 31 Jan 2013
This looks like a good thing to do also, thanks. For all intents and purposes the numbers I am getting -0.00000 for could be equated to zero.

Sign in to comment.

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!