问题描述:

I have a list of doubles in the range of anywhere between -1.396655 to 1.74707 could even be higher or lower, either way I would know what the Min and Max value is before normalizing. My question is How can I normalize these values between -1 to 1 or even better yet convert them from double values to char values of 0 to 255

Any help would be appreciated.

double range = (double)(max - min);

value = 255 * (value - min)/range

网友答案:

The easiest thing is to first shift all the values so that min is 0, by subtracting Min from each number. Then multiply by 255/(Max-Min), so that the shifted Max will get mapped to 255, and everything else will scale linearly. So I believe your equation would look like this:

newval = (unsigned char) ((oldval - Min)*(255/(Max-Min)))

You may want to round a bit more carefully before casting to char.

网友答案:

You need a mapping of the form y = mx + c, and you need to find an m and a c. You have two fixed data-points, i.e.:

 1 = m * max + c
-1 = m * min + c

From there, it's simple algebra.

相关阅读:
Top