【发布时间】:2013-01-23 12:59:36
【问题描述】:
我有两个大小相同的列表。两者都包含数字。第一个列表是生成的,第二个是静态的。由于我有许多生成的列表,我想找出哪一个是最好的。对我来说,最好的清单是最接近参考的清单。因此,我计算每个位置的差异并将其相加。
代码如下:
/// <summary>
/// Calculates a measure based on that the quality of a match can be evaluated
/// </summary>
/// <param name="Combination"></param>
/// <param name="histDates"></param>
/// <returns>fitting value</returns>
private static decimal getMatchFitting(IList<decimal> combination, IList<MyClass> histDates)
{
decimal fitting = 0;
if (combination.Count != histDates.Count)
{
return decimal.MaxValue;
}
//loop through all values, compare and add up the result
for (int i = 0; i < combination.Count; i++)
{
fitting += Math.Abs(combination[i] - histDates[i].Value);
}
return fitting;
}
是否有更优雅但更重要和更有效的方法来获得所需的总和?
提前致谢!
【问题讨论】:
-
如果要比较的列表数量很大,那么由于您只想要最佳匹配,您可以使用'best sofar'的差异作为参数,一旦当前列表有一个就停止比较更大的差异。
标签: c#