开发者

calculate the standard deviation of a generic list of objects [duplicate]

This question already has answers here: How do I determine the standard deviation (stddev) of a set of values? (12 answers) Closed 9 years ago.

I'm a c# noob but I really need a professional's help. I am using visual studio 2005 for a project so I don't 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 s开发者_运维百科how me that has calculated standard deviation of a generic list before. Here is my code that I tried to start on:

//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;
    }
}

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

That's it. Any help would much be appreciated. Thanks


Try

public float LatchTimeStdev()
{
    float mean = LatchTimeMean();
    float returnValue = 0;
    foreach (ValveData value in m_ValveResults)
    {
        returnValue += Math.Pow(value.LatchTime - mean, 2); 
    }
    return Math.Sqrt(returnValue / m_ValveResults.Count-1));
}

Its just the same as the answer given in LINQ, but without LINQ :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜