2016-11-04 5 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

입니다. http://stackoverflow.com/questions/262934/is-it-costly-to-do-array-length-or-list-count-in-a-loop – Eric

+0

@Eric, hmmm ... 맞습니다! – 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

이 만족스러운 해답이 될 것 같다, 그래서 가서 하나를 게시 할 수 있습니다. 이미 LINQ가 빠른 알고하거나 내부 foreach는 내부에`카운트 ++ '추가하지, 루프를하고있는 것처럼 내가 생각

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