Scale a list of numbers from -1.0 to 1.0

Johnny picture Johnny · Aug 11, 2010 · Viewed 13.3k times · Source

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?

Answer

deinst picture deinst · Aug 11, 2010

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.