2010-06-30 89 views
2

我是c#noob,但我真的需要专业人士的帮助。我为一个项目使用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; 
    } 
} 

的“锁定时间”是指插入m_ValveResults列表中的“ValveData”对象的浮动对象。

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

+5

欢迎#1。你有一个很好的问题,但为了将来的参考,“PLZ HELP !! ?? @ 11”不是一个好问题标题。 – 2010-06-30 14:25:26

+3

为什么你会接受[这个答案](http://stackoverflow.com/questions/3141692/c-standard-deviation-of-generic-list/3141731#3141731),然后再问同样的问题? – 2010-06-30 14:32:13

+1

@John - 我注意到在这个问题中他指定他使用的是VS 2005,所以他不能使用Linq。 – Greg 2010-06-30 14:36:19

回答

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 :)

+0

谢谢。但什么是“总和”?和values.Count?谢谢你的帮助。这是更多的即时通讯寻找。 – 2010-06-30 14:55:19

+0

对不起,编辑它才能正常工作 – fbstj 2010-06-30 19:36:39