Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the appropriate collection for calculating a running mean?
    text
    copied!<p>I'm sifting through some of my old bugs and while reviewing some nasty code I realized that my averaging or smoothing algorithm was pretty bad. I did a little research which led me to the <a href="http://www.numberwatch.co.uk/smoothing_of_data.htm" rel="nofollow">"running mean"</a> - makes sense, pretty straightforward. I was thinking through a possible implementation and realized that I don't know which collection would provide the type of "sliding" functionality that I need. In other words, I need to push/add an item to the end of the collection and then also pop/remove the first item from the collection. I think if I knew what this was called I could find the correct collection but I don't know what to search for.</p> <p>Ideally a collection where you set the max size and anything added to it that exceeds that size would pop off the first item.</p> <p>To illustrate, here is what I came up with while messing around:</p> <pre><code>using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { LinkedList&lt;int&gt; samples = new LinkedList&lt;int&gt;(); // Simulate packing the front of the samples, this would most like be a pre-averaged // value from the raw samples for (int i = 0; i &lt; 10; i++) { samples.AddLast(0); } for (int i = 0; i &lt; 100; i++) { // My attempt at a "sliding collection" - not really sure what to call it but as // an item is added the first item is removed samples.RemoveFirst(); samples.AddLast(i); foreach (int v in samples) { Console.Write("{0:000} ", v); } Console.WriteLine(String.Empty); } Console.ReadLine(); } } } </code></pre> <p>As you can see I am manually handling the removal of the first item. I'm just asking if there is a standard collection that is optimized for this type of use?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload