Click here to Skip to main content
15,881,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Guys. Im a c# noob but I really need a professional's help. I am using visual studio 2005 for a project so I dont have math.linq I need to calculate the standard deviation of a generic list of objects. The list contains just a list of float numbers, nothing too complicated. However I have never done this before so i need someone to show me that has calculated standard deviation of a generic list before. Here is my code that I tried to start on:

C#
//this is my list of objects inside. The valve data just contains a bunch of floats.
public class ValveDataResults
{
    private List<valvedata> m_ValveResults;

    public void AddValveData(ValveData valve)
    {
        m_ValveResults.Add(valve);
    }
    
    //this is the function where the standard deviation needs to be calculated:
    public float LatchTimeStdev()
    {
        //below this is a working example of how to get an average or mean
        //of same list and same "LatchTime" that needs to be calculated here as well. 
    }

    //function to calculate average of latch time, can be copied for above st dev.
    public float LatchTimeMean()
    {
        float returnValue = 0;
        foreach (ValveData value in m_ValveResults)
        {
            returnValue += value.LatchTime; 
        }
        returnValue = (returnValue / m_ValveResults.Count) * 0.02f;
        return returnValue;
    }</valvedata>


the "Latch Time" is a float object of the "ValveData" object which is inserted into the m_ValveResults list.

thats it. any help would much be appreciated. Thanks

[Modified: if you add <pre lang="cs"> before the code and </pre> after the code, it will format it for you!]
Posted
Updated 30-Jun-10 4:32am
v3

1 solution

A simple google search of c# and standard deviation gave this:

C#
private double getStandardDeviation(List<double> doubleList)
{
   double average = doubleList.Average();
   double sumOfDerivation = 0;
   foreach (double value in doubleList)
   {
      sumOfDerivation += (value) * (value);
   }
   double sumOfDerivationAverage = sumOfDerivation / doubleList.Count;
   return Math.Sqrt(sumOfDerivationAverage - (average*average));
}
 
Share this answer
 
Comments
Tom Hangler 30-Jun-10 10:44am    
many things wrong with this. First as I mentioned, I have VS 2005 which wont support math.Linq therefore no Average. Also I wont be passing anything through parameters. And mine is float not double. Thanks for da help but I need a little more. Possible someone who did this sort of thing before
William Winner 30-Jun-10 10:56am    
Regardless of what you're using, there is a math library available for pretty much every language. If you're using c#, then you definitely have System.Math. If you didn't, that would would surprise me. Math.Linq isn't necessary.

Secondly, a double is just a larger float. If you can't change the word double in that code to float, then you're not going to get far in programming.

Thirdly, if you can't figure out how to write your own Average, then again, not going to get far.
Tom Hangler 30-Jun-10 11:01am    
thank you. it helped a lot.
Tom Hangler 30-Jun-10 11:01am    
i figured it out. Sorry for confusion. thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900