我是一个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;
}
}
“Latch Time”是“ValveData”对象的float对象,它插入到m_ValveResults列表中。
就是这样。任何帮助将不胜感激。感谢
答案 0 :(得分: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:)