필터 지우기
필터 지우기

normalize between -1 > x > 1 (not equal to -1 or 1)

조회 수: 2 (최근 30일)
Nuchto
Nuchto 2017년 11월 26일
댓글: Walter Roberson 2017년 12월 20일
How to normalize values so that they fall within -1 and 1 (but no values should be equal either -1 or 1). Thank you.

채택된 답변

John D'Errico
John D'Errico 2017년 11월 26일
편집: John D'Errico 2017년 11월 26일
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
Nuchto
Nuchto 2017년 12월 20일
편집: Walter Roberson 2017년 12월 20일
@John D'Errico: One question: in your answer https://www.mathworks.com/matlabcentral/answers/369292-normalize-between-1-x-1-not-equal-to-1-or-1#comment_509253, you said that the last step of multiplying by (1-eps) contracts the restful by that amount. I wonder if one could just subtract eps from the resulting vector instead of multiplying. It feels more intuitive to me. Let me know whether this is wrong. Thank you!
Walter Roberson
Walter Roberson 2017년 12월 20일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by