首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

帮小弟我看个LINQ的函数,万分感谢

2012-04-17 
帮我看个LINQ的函数,万分感谢C# code// returns the nearest center to the input vector, using the L2 n

帮我看个LINQ的函数,万分感谢

C# code
        // returns the nearest center to the input vector, using the L2 norm        public static double[] NearestCenter(double[] vector, double[][] centers)        {            // written in LINQ to "prove it can be done". easier to see that it is functional, too            return centers.Aggregate(centers[0], (old, cur) => (vector.Select((v, i) => v - old[i])                                                                      .Select(diff => diff * diff)                                                                      .Sum() >                                                                vector.Select((v, i) => v - cur[i])                                                                      .Select(diff => diff * diff)                                                                      .Sum()) ? cur : old);        }


L2 norm好像是求 坐标系内两点之间的距离的。

还有这里的old和cur代指什么?

[解决办法]
探讨
C# code


// returns the nearest center to the input vector, using the L2 norm
public static double[] NearestCenter(double[] vector, double[][] centers)
{
// ……

还有这里的old和cur代指什么?

[解决办法]
探讨
引用:

这要搞明白 Aggregate 这个函数的作用。

它会每次提供给你它遍历这个数据集合获取的上一条记录和本条记录。old(这个英语单词用的够蹩脚的,应该用previous) 是上一条,cur(rent)是本条。

这个是MSDN官方的代码。
old 和cur是遍历的是 centers[0]里面的元组吗?

[解决办法]
修改下:
C# code
T Aggregate<T>(this IEnumerable<T> collection, T first, Func<T, T, T> foo){    T old = first;    T r;    foreach (var cur in collection)    {        r = foo(old, cur);        old = cur;    }    return r;} 

热点排行