This should be an easy one:
I have a list of numbers. How do I scale them to be between -1.0 and 1.0 such that the smallest value is -1.0 and the largest is 1.0?
This should be an easy one:
I have a list of numbers. How do I scale them to be between -1.0 and 1.0 such that the smallest value is -1.0 and the largest is 1.0?
Find the min and the max
then for each number scale x
to 2 * (x - min)/( max - min) - 1
Just to check --
If it is a long list precomputing c = 2/(max - min)
and scaling with 'c * x - 1` is a good idea.
This is a signed normalization
1 - get the Minimum and Maximum value on the list (MinVal,MaxVal)
2 - convert each number using this expressions signedNormal = (((originalNumber - Minimum) / (Maximum - Minimum)) * 2.0) - 1.0
I deliberately made this inefficient in order to be clear - more efficient would be
double min = myList.GetMinimum();
double max = myList.GetMaximum();
double signedRangeInverse = 1.0 / (max - min);
for(int i = 0;i < myList.NumberOfItems();i++)
myList[i] = (((myList[i] - min) * signedRangeInverse) * 2.0) - 1
No point in recalculating range each time No point in dividing range, mult is faster
If you want 0 to still equal 0 in the final result:
E.g
[ -5, -3, -1, 0, 2, 4]
Number with largest magnitude is -5. We can get that to equal -1 by multiplying by 0.2 (-1 / -5). (Beware of divide by 0s if your numbers are all 0s.)
So multiply all the elements by 0.2. This would give:
[-1, -0.6, -0.2, 0, 0.4, 0.8]
Although note that
[ -5, -5, -5 ] -> [ -1, -1, -1 ]
and
[ 5, 5, 5 ] -> [ 1, 1, 1 ]
and
[ 0, 0, 0 ] -> [ 0, 0, 0 ]
That may or may not be what you want. Thanks to @Hammerite for prompting me on that one with his very helpful comment :)