确定从文件中提取的值是否在一个范围内

时间:2011-09-21 14:46:18

标签: c# linq linq-to-xml range standard-deviation

所以我必须编写一个代码来确定从文件中提取的值是否落在特定范围内。

                double averageMaximumX = query.Average(t => double.Parse(t.XMax));
   double varianceMaximumX = query.Sum(t => Math.Pow(double.Parse(t.XMax) - averageMaximumX, 2));
                varianceMaximumX /= query.Count();
                double stdDevMaximumX = Math.Sqrt(varianceMaximumX);

                double averageMinimumX = query.Average(t => double.Parse(t.XMin));
   double varianceMinimumX = query.Sum(t => Math.Pow(double.Parse(t.XMin) - averageMinimumX, 2));
                varianceMinimumX /= query.Count();
                double stdDevMinimumX = Math.Sqrt(varianceMinimumX);

                double averageMaximumY = query.Average(t => double.Parse(t.YMax));
   double varianceMaximumY = query.Sum(t => Math.Pow(double.Parse(t.YMax) - averageMaximumY, 2));
                varianceMaximumY /= query.Count();
                double stdDevMaximumY = Math.Sqrt(varianceMaximumY);

                double averageMinimumY = query.Average(t => double.Parse(t.YMin));
   double varianceMinimumY = query.Sum(t => Math.Pow(double.Parse(t.YMin) - averageMinimumY, 2));
                varianceMinimumY /= query.Count();
                double stdDevMinimumY = Math.Sqrt(varianceMinimumY);

                double averageMaximumZ = query.Average(t => double.Parse(t.ZMax));
   double varianceMaximumZ = query.Sum(t => Math.Pow(double.Parse(t.ZMax) - averageMaximumZ, 2));
                varianceMaximumZ /= query.Count();
                double stdDevMaximumZ = Math.Sqrt(varianceMaximumZ);

                double averageMinimumZ = query.Average(t => double.Parse(t.ZMin));
   double varianceMinimumZ = query.Sum(t => Math.Pow(double.Parse(t.ZMin) - averageMinimumZ, 2));
                varianceMinimumZ /= query.Count();
                double stdDevMinimumZ = Math.Sqrt(varianceMinimumZ);


                var results = from item in query
                              select new
                              {
       XMaxResult = TryParseWithDefault(item.XMax, double.NaN) <= averageMaxX ? "pass" : "FAIL",
       XMinResult = TryParseWithDefault(item.XMin, double.NaN) >= averageMinX ? "pass" : "FAIL",
       YMaxResult = TryParseWithDefault(item.YMax, double.NaN) <= averageMaxY ? "pass" : "FAIL",
     YMinResult = TryParseWithDefault(item.YMin, double.NaN) >= averageMinY ? "pass" : "FAIL",
       ZMaxResult = TryParseWithDefault(item.ZMax, double.NaN) <= averageMaxZ ? "pass" : "FAIL",
        ZMinResult = TryParseWithDefault(item.ZMin, double.NaN) >= averageMinZ ? "pass" : "FAIL"

                              };

1 个答案:

答案 0 :(得分:0)

将标准偏差加到并减去平均值。

实施例

XMaxResult = (TryParseWithDefault(item.XMax, double.NaN) <= averageMaxX + stdDevMaximumX 
             && TryParseWithDefault(item.XMax, double.NaN) >= averageMaxX - stdDevMaximumX)  
             ? "pass" : "FAIL"