【问题标题】:calculate the standard deviation of a generic list of objects [duplicate]计算通用对象列表的标准偏差[重复]
【发布时间】:2011-03-10 04:10:09
【问题描述】:

我是一个 c# 菜鸟,但我真的需要专业人士的帮助。我正在为一个项目使用 Visual Studio 2005,所以我没有 math.linq 我需要计算通用对象列表的标准偏差。该列表仅包含一个浮点数列表,没有什么太复杂的。但是我以前从来没有这样做过,所以我需要有人告诉我以前计算过通用列表的标准偏差。这是我尝试开始的代码:

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

“Latch Time”是插入到 m_ValveResults 列表中的“ValveData”对象的浮点对象。

就是这样。任何帮助将不胜感激。谢谢

【问题讨论】:

  • 欢迎来到 Stackoverflow。您有一个很好的问题,但为了将来参考,“PLZ HELP!!??@11”不是一个好的问题标题。
  • 你为什么要接受this answer然后再问同样的问题?
  • @John - 我注意到在这个问题中他指定他正在使用 VS 2005。所以他将无法使用 Linq。
  • @Greg:那么他应该不接受该答案并修改该问题以禁止 linq。

标签: c# math visual-studio-2005 statistics


【解决方案1】:

试试

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

它与 LINQ 中给出的答案相同,但没有 LINQ :)

【讨论】:

  • 谢谢。但什么是“总和”?和值。计数?谢谢你的帮助。这更适合我正在寻找的东西。
  • 对不起,编辑它以正常工作
猜你喜欢
  • 2013-06-20
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多