normalize between -1 > x > 1 (not equal to -1 or 1)
1 view (last 30 days)
Show older comments
How to normalize values so that they fall within -1 and 1 (but no values should be equal either -1 or 1). Thank you.
0 Comments
Accepted Answer
John D'Errico
on 26 Nov 2017
Edited: John D'Errico
on 26 Nov 2017
You can't do this. Not effectively. Ok, let me say that you cannot do this well. Strict inequalities are not easily used when working with floating point numbers. You should essentially never test for something exactly equal to a number anyway in floating point arithmetic.
So I don't know what you are trying to do. But it is a bad idea in general. Have I convinced you?
Sigh, since I know someone will give you an answer anyway, I might as well offer a decent one, something that should work. Are you asking for a linear normalization? Thus a linear shift and scale of the vector elements?
x= randn(1,100);
format long g
min(x)
ans =
-2.0648600364807
max(x)
ans =
3.56986777687433
Now, how to normalize x as you apparently wish. This would normalize a vector to +/- 1. But floating point arithmetic being involved, there is no assurance that a strict inequality will exist. In fact, it might even result in something less than -1 or greater than +1 by some amount on the order of eps.
xnorm = (x - min(x))/(max(x) - min(x))*2 - 1;
min(xnorm) == -1
ans =
logical
1
max(xnorm) == 1
ans =
logical
1
In fact, in this case, it hit +/-1 on the nose. To try to yield a strict inequality, I'll do one extra step.
xnorm = ((x - min(x))/(max(x) - min(x))*2 - 1)*(1-eps);
This time, I multiplied by 1-eps. That contracts the result by just enough that the min and max elements will be closer to zero by the smallest amount possible.
min(xnorm) == -1
ans =
logical
0
max(xnorm) == 1
ans =
logical
0
min(xnorm) +1
ans =
2.22044604925031e-16
max(xnorm) - 1
ans =
-2.22044604925031e-16
So the min is just above -1 by eps. The max is just below 1 by eps. All is good in the world, at least for today. To safer, I might perhaps have chosen to contract things by a factor of (1-2*eps) instead of (1-eps).
Regardless, trying to work with strict inequalities like this will lead you into trouble one day. One day, not far into the future we may see an anguished question from you, asking why your code does not work properly.
11 Comments
Walter Roberson
on 20 Dec 2017
Consider the signal that is exactly -1 . If you subtract eps then you get -1-eps which is less than -1 and so would be outside the range.
You could do a substitution on two values,
mapped_signal(mapped_signal == -1) = -1+eps;
mapped_signal(mapped_signal == 1) = 1-eps;
Another way of writing this would be
mapped_signal = min( max(mapped_signal, -1+eps), 1-eps );
This is not mathematically linear but it might be acceptable for your purposes.
But the mathematically linear version is easy to write
mapped_signal = mapped_signal * (1-eps);
More Answers (0)
See Also
Categories
Find more on Logical 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!