ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Suppose I have the following array to generate a table. How can I perform calculation to get the moving average of last 10 collections on the 11th row (not the 10th)? I managed to pull the index value for each array but have no clue how to go further. Also, any suggestions to improve the code is most appreciated.
This can't be difficult, not even in PHP, but you should clarify your question. Now it is fully incomprehensible.
Quote:
Originally Posted by pcock
moving average of last 10 collections on the 11th row (not the 10th)?
Which 10 collections? Do you mean "a collection of 10 values in this array"?
Quote:
Originally Posted by pcock
pull the index value for each array but have no clue how to go further. Also, any suggestions to improve the code is most appreciated.
Each array? I see only one array.
Please make clear what your intention is, give values and examples of what you want to calculate. When that is fully clear, translating it into PHP is a breeze.
This should come close. Bear in mind that with a moving average you'll always having difficulties at the start and end of the data range as a moving average assumes a continuous data stream.
/*
* We crate a new array with the same indices as $amounts,
* but then for the average. This could be done in the same
* array as well by making it two-dimensional.
* Build the first sum:
*/
$sum = 0;
for ($i = 0; $i < $AVG_WINDOW; $i++){
$sum = $sum + $amounts[$i];
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.