note 54219 deleted from ref.array by aidan

From: Date: Sun, 09 Oct 2005 09:12:58 +0000
Subject: note 54219 deleted from ref.array by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-96729@lists.php.net to get a copy of this message
Note Submitter: bob at scl dot com ---- /* Ugly but 'good enough' method of deriving a moving average array from a source array. no type checking. not very efficient possibily very inaccurate good enough (I hope) to do moving averages for a jpgraph plot */ function moving_average($input_array,$window) { $output_array = array(); for ($i=0; $i<count($input_array); $i++) { $maverage=$window; if($i+1<$maverage) $maverage=$i+1; $output_array[$i]=array_sum(array_slice($input_array, $i-$maverage+1 , $maverage))/$maverage; } return $output_array; } // example use $input = array(1,1,1,1,5,5,5,5); $output = moving_average($input,3); print_r($output); /* which should produce something like : Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 2.3333333333333 [5] => 3.6666666666667 [6] => 5 [7] => 5 ) which seems reasonable */

« previous php.notes (#96729) next »