MATLAB: Normalize between -1 > x > 1 (not equal to -1 or 1)

normalize

How to normalize values so that they fall within -1 and 1 (but no values should be equal either -1 or 1). Thank you.

Best Answer

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.