Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale a list of numbers from -1.0 to 1.0

This should be an easy one.

I have a list of numbers. How do I scale list's values, between -1.0 and 1.0 in order for min = -1 and max = 1.0?

like image 412
Johnny Avatar asked Aug 11 '10 17:08

Johnny


3 Answers

If you want 0 to still equal 0 in the final result:

  1. Find the number with the largest magnitude. This will either map to 1 or -1.
  2. Work out what you need to multiply it by to make it 1 or -1.
  3. Multiply all the numbers in the collection by that factor.

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 :)

like image 156
Alex Humphrey Avatar answered Oct 19 '22 23:10

Alex Humphrey


Find the min and the max

then for each number scale x to 2 * (x - min)/( max - min) - 1

Just to check --

  1. min scales to -1
  2. and max scales to 1

If it is a long list precomputing c = 2/(max - min) and scaling with c * x - 1 is a good idea.

like image 27
deinst Avatar answered Oct 19 '22 22:10

deinst


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

like image 39
Mark Mullin Avatar answered Oct 19 '22 22:10

Mark Mullin