[Tutor] scaling values
kent37 at tds.net
Wed Mar 15 03:51:51 CET 2006
kevin parks wrote:
> Seems my post added much confusion. Sorry... I was hoping not to have
> to post my code since it is really wrong and slightly embarrassing..
I think the confusion was about what input range to use. From your code
it looks like you want to use just the actual range of input values.
> what i am trying to do is map an input range of values to output range.
> I was hoping to make it a bit of an all purpose utility that would map
> pretty much any input range to an output range, also do inverted
> mapping... and also handle negative numbers and perhaps even a flag for
> exponential mapping.
> import random
> def scaleX(in_seq, low, hi):
> range1 = max(in_seq) - min(in_seq)
> #range2 = max(out_seq) - min(outseq)
> range2 = hi - low
> ratio = range1/range2
> return [(x * ratio) for x in in_seq]
This is actually pretty close. You have ratio backwards and you need to
account for the offsets min(in_seq) and low. Try
in_low = min(in_seq)
ratio = range2/range1
return [ ((x-in_low) * ratio + low) for x in in_seq ]
More information about the Tutor