[英]Normalize a negative or positive number between -1 and 1?
I have streaming data, as float values that range between -2 and +4.我有流数据,浮点值介于 -2 和 +4 之间。 I need to write a function that normalizes these values between -1 and +1.
我需要编写一个函数,将这些值在 -1 和 +1 之间标准化。
I have:我有:
float normalize(float input)
{
int min = -1;
int max = 1;
float normalized_x = (input - min) / (max - min);
return normalized_x;
}
But this gives me values that are incorrect, and range from -0.4 to +2.3, roughly.但这给了我不正确的值,大致范围从 -0.4 到 +2.3。 What do I need to adjust in my function?
我需要在我的函数中调整什么?
Thank you.谢谢你。
You want to first center the range around 0, then divide to make it go from -1 to 1.您希望首先将范围集中在 0 附近,然后除以使其从 -1 到 1。
float normalize(float input)
{
float normalized_x = (input - 1) / 3;
return normalized_x;
}
More generalized:更概括:
const float min = -2;
const float max = 4;
float normalize(float input)
{
float average = (min + max) / 2;
float range = (max - min) / 2;
float normalized_x = (input - average) / range;
return normalized_x;
}
是的,但是它是一种标准方法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.