Page 1 of 1

Maths

Posted: Thu Sep 02, 2004 8:49 am
by Grim...
Okay, I'm not really sure where to put this, so I guess I can't go wrong in here.

Here is the deal.

I have a group of numbers, say 1, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 6, 7, 7, 8.
I want the smallest number to equal 10, and the highest to equal 20, and I want all the numbers inbetween to get the relevent number between 10 and 20 (in this case, 4 would euqal 15 and 6 would equal 17.5).

The highest and lowest numbers could change, so I could have a group of numbers starting with 10000 and ending with 909443 but they (and all the numbers inbetween) would still have to go into the 10 - 20 scale.

So how the hell do I do that?

Posted: Thu Sep 02, 2004 9:39 am
by feyd
I'm not sure what you are asking.. so lemme try this: you want to pass an array of numbers, that you want scaled into the range you specify?

Posted: Thu Sep 02, 2004 9:47 am
by Draco_03
small number always = to 10, biggest number always equals to 20..
BUT what he wants is that every number between will be relevent
meanin if i have 2 as the smallest number 4 as a biggest number andi have 3 in between (meaning i got 2, 3, 4)
2 will become 10
4 will become 20
3 will become 15

Why 3 is 15..
well be simple calculation, for 2 tobecom 10, and for 4 to becom 20 we have to multiply everything by 5
so if we have to scale 3 with the same frmula, (x5) it'll become 15.

Posted: Thu Sep 02, 2004 11:25 am
by Grim...
That's fine Draco, but what if the numbers are 1, 6, 250, 500?

There must be a formula for it - where are the Math phd boys?

Posted: Thu Sep 02, 2004 11:28 am
by Grim...
Feyd - yes.

What I want it for is a site that has pictures that you can tag with keywords (called tags).
This page takes the keywords and adds them up, then displays them in alphabetical order, the rarer ones with a size of 10px and the most common ones with the size of 20px.

Linkage

Posted: Thu Sep 02, 2004 11:30 am
by Grim...
Although I might want to make the max size 35px or something - but once the formula is worked out I'm sure it won't be that hard.

At the moment it looks like it's working, but that's because the max number is 25ish. If it gets to 250:
a) The letters will be huge
b) There will be so many pictures I'll have run out of webspace ;)

Posted: Thu Sep 02, 2004 11:46 am
by feyd
k.. so here's the basics: (untested)

Code: Select all

function distribute($array, $min, $max)
{
  $array = sort($array);
  $low = $array[0];
  $high = $array[sizeof($array) - 1];
  $scale = ((float)($max - $min))/($high - $low);
  $return = array();
  foreach($array as $key) $return[$key] = ($key - $low) * $scale + $min;
  return $return;
}
I believe the math is in order.. :)

Posted: Thu Sep 02, 2004 12:11 pm
by Draco_03
My previous post just explained to feyd your first post
:)

Posted: Thu Sep 02, 2004 12:21 pm
by feyd
as I said, untested.

Posted: Thu Sep 02, 2004 12:22 pm
by Christopher
feyd may have it right. To to it the long way:

0. Find the Min value.

1. Make the range zero based by subtracting the Min from every number.

2. Find the Max value.

3. Then calculate your Scale, which in your case is Max / 10.

5. Then normalize all the numbers my dividing them by Scale. That give you numbers in the range >0 and <10.

5. Finally add an Offset to every number to put them in the range you want. In your case add 10 to get them in the range >10 and <20.

There are definitely faster ways to do it, but it is so weird that I would do it the long way and document the code well.

Posted: Thu Sep 02, 2004 3:43 pm
by timvw
and realise that $max can equal $min.. (found this bug in panachart few days ago ;))

else dy = $value / $max - $min = $value / 0 -> error :)

Posted: Thu Sep 02, 2004 3:48 pm
by feyd
$max = $min isn't a problem. Only $high = $low can be a problem. Course if computers would return infinite results instead of exploding when dividing by zero, things would be slightly more fun... :?

Posted: Fri Sep 03, 2004 2:47 am
by Grim...
Thanks guys!