Rate this script:  I Love it  /   I Hate it

useful normalizing functions (php)


Code


<?
function normalize($arr,$LO=0.01,$HI=0.99)
{
  $Min2147483647;
  $Max = -2147483647;
  for ($a=0; $a<count($arr); $a++) {
    $Min = min($Min, $arr[$a]);
    $Max = max($Max, $arr[$a]);
  }
  $Mean = 0;
  for ($a=0; $a<count($arr); $a++) {
    $div = $Max-$Min;
    if($div == 0){$div = 1;}
    $arr [$a] = (($arr[$a]-$Min) / ($div)) * ($HI-$LO)   $LO;
    $Mean  = $arr[$a] / count($arr);
  }
  return $arr;
}


/*
    dual-way recursive greedy grabber..
    begins at a position in an array and grabs all "in order" parts up and
    down from that point and returns them preserving keys.
*/

function section_greedy($stack, $start=0, $stop=2){
    $check2 = array_slice_key ($stack, $start-1, $stop);
    if ($start >= 0 && $stop <= count($stack) 1){ // limit recursion to bounds.
    if (is_in_rorder_key($check2)){
        $check$check2;
        $begin = $start-1; $ending = $stop;       
        $check3 = section_greedy($start-1, $stop 1); //recurse up
        if(is_in_rorder_key($check3)){
            $check = $check3;
            $moremore = section_greedy($start, $stop 1); //recurse down
            if(is_in_rorder_key($moremore)){
                $check$moremore;
                $begin = $start-2; $ending = $stop 2;
            }//fi
        }//fi
    }else{
        $check =  array_slice_key($stack, $start, $stop);
        $begin = $start; $ending = $stop;
    }//fi
    }//fi
    return $check;
}//end function

/*
returns an array of movements up/down and the change.
Array(
    [down] => 1178.36
    [up] => 419.05
    [change] => -759.31
)
*/

function examine_movement($arr){
    while(list($key,$val) = each($arr)){
        if($oldval == ""){$oldval = $val;}
        if($val > $oldval){
            $ret[up] += $val-$oldval;
        }else{
            $ret[down] += $oldval-$val;
        }
        $oldval = $val;       
    }//wend
    $ret[change] = $ret[up] - $ret[down];
    return $ret;
}

?>
 

 

 
useful normalizing functions (php) scripts | useful normalizing functions (php) snippet | useful normalizing functions (php) example | useful normalizing functions (php) tutorial | useful normalizing functions (php) code