2016-11-04 53 views
1
public static double PrazenWindowDensity(double [][] Xn, double x, double sigma2) 
    { 
     double gauss = 0; 

     foreach(double [] arr in Xn) 
     { 
      foreach (double item in arr) 
      { 
       double xx = GausianFunction(item, x, sigma2); 
       gauss += xx; 
      } 
     } 

     return gauss/Xn.Length; //this is surely incorrect. Isn't it? 
    } 

我可以在這裏寫什麼?尋找2D鋸齒陣列中元素數量的標準方法是什麼?

return gauss/Xn.Length; 
+2

正如你已經在做一個循環,不知道LINQ是更快或只加內的foreach內'計數++'。 http://stackoverflow.com/questions/262934/is-it-costly-to-do-array-length-or-list-count-in-a-loop – Eric

+0

@Eric,嗯......正確的你! – anonymous

回答

1

最快和最簡單的方法我認爲是

public static double PrazenWindowDensity(double[][] Xn, double x, double sigma2) 
{ 
    double gauss=0; 
    int count=0; 
    for (int i=0; i<Xn.Length; i++) 
    { 
     gauss+=Xn[i].Sum((item) => GausianFunction(item, x, sigma2)); 
     count+=Xn[i].Length; 
    } 
    return gauss/count; 
} 
1

這似乎是一個滿意的答覆,所以我會繼續和張貼爲一體。

return gauss/Xn.Sum(x => x.Length);