note 54219 added to ref.array
| From: | bob at scl dot com | Date: | Mon, 27 Jun 2005 15:48:38 +0000 |
| Subject: | note 54219 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91031@lists.php.net to get a copy of this message | ||
/*
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
*/
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54219
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54219&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54219&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54219&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54219&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54219&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54219&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54219&report=yes
Search -- http://master.php.net/manage/user-notes.php