Scaling Values โ€‹โ€‹in PHP

I can not get around how to express this PHP.

I have 100 and 420 as my min and max value I want to get.

What suggests:

1000 4534 34566 123145 12312265 

Now, how can I say:

Take 4534 and knowing that 1000 = 420 and 12312265 = 100 determine the value of 4534.

To make this clearer, I am trying to imagine ranking web pages with squares, so if the rank is 1, it should be converted to my maximum value / size of 420, however, if the page is of low popularity, say 13000, then its size should be close to a minimum of 100. I know all the values.

Thanks.

Itโ€™s still hard for me to understand this.

So far, using the code from the first answer, I:

 $srcmin=1185; $srcmax=25791525; $destmin=100; $destmax=420; $pos = (($RANK - $srcmin) / ($srcmax-$srcmin)) ; $rescaled = ($pos * ($destmax-$destmin)) + $destmin;*/ $percentage = (($RANK - $MIN) * 100) / $MAX; $SIZE = (($percentage / 320) * 100) + 100 

Being $ RANK, my values โ€‹โ€‹for web page series and $ SIZE are the value I need for their correct size. This does not work (my mistake, no doubt) everything I get from $ SIZE is 100.

+7
math php scale
source share
4 answers

This should illustrate ....

 $values=array(1000, 4534, 34566, 123145, 12312265); $srcmin=$values[0]; $srcmax=$values[count($values)-1]; $destmin=420; $destmax=100; foreach($values as $x) { //how far in the source range is $x (0..1) $pos = (($x - $srcmin) / ($srcmax-$srcmin)) //figure out where that puts us in the destination range $rescaled = ($pos * ($destmax-$destmin)) + $destmin; } 

You want to know how far in the original range each number is used, which gives the value of $ pos. Given this, you can translate this to how far you have reached your destination range.

+5
source share

What you need to do is

(1) find [0..1] the position of each value in the range of values, 1000 means x = 0 and 123145, which means x = 1;

(2) invert it because you want the smallest number to have the largest width (x = 1-x)

(3) translate this number [0..1] into the desired width range.

 $values = array(1000, 4534, 34566, 123145); $minV = min($values); $maxV = max($values); $minW = 100; $maxW = 420; foreach($values as $v) { $width = $minW + ($maxW - $minW) * (1 - (($v - $minV) / ($maxV - $minV))); echo "<div style='width:".$width."px;background-color:red;padding:5px;'></div>"; } 

or you can generalize it to a function:

 function invTranslate($value) { $min1 = 1000; $max1 = 123145; $min2 = 100; $max2 = 420; return $min2 + ($max2 - $min2) * (1 - (($value - $min1) / ($max1 - $min1))); } 

and use it like:

 <div style="width:<?=invTranslate(5000)?>px"></div> <div style="width:<?=invTranslate(100000)?>px"></div> <div style="width:<?=invTranslate(90000)?>px"></div> 
+1
source share

I think you mean

 $min=100; $max=420; $range = $max - $min; $nums = [10,15,20,25,30]; // Edit this as you please $relMin = min($nums); $relMax = max($nums); $relRange = $relMax - $relMin; foreach($nums as $num) { $pct = ($num - $relMin)/$relRange; echo "$num is %$pct between $relMin and $relMax"; $val = $pct*$range + $min; echo "$pct of the range between $min and $max is $val"; } 
0
source share

First, determine the minimum and maximum values โ€‹โ€‹of the data that you have. Then your question is equivalent to this -

We will call MIN your target minimum and MAX your target maximum. We will call val_min your minimum value and val_max your maximum value.

Then you need to find both ranges - RANGE = MAX-MIN , val_range = val_max - val_min

Now you can start the conversion.

First, convert the converted value number to a number from 0 to 1, with val_min matching with 0 and val_max matching with 1:

 normed_value = (value - val_min) / val_range 

Then map the value from 0 to 1 to the final range:

 final_scaled_value = MIN + (normed_value * RANGE) 
0
source share

All Articles