2010-06-30 39 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