开发者

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 a开发者_JAVA百科nd 1.0 in order for min = -1 and max = 1.0?


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.


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:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜