2011-01-05 39 views
3

linqでセグメント化されたヒストグラムを作成する方法はありますか?私は、特定のオブジェクトの出現回数を数えることができるいくつかの例を見てきました。 2つの値の間の一連のオブジェクトの出現回数を数えるlinqベースのヒストグラムを作成できますか?Linqによるヒストグラム

ヒストグラムに必要なバケットを作成するためにアイテムの範囲をどのようにグループ化するのか分かりませんか?開始範囲と幅を使用して範囲を作成するとします。

数値配列を繰り返し、各数値を< =上界と下界>かどうかでグループ化する必要があります。それから、各グループを合計します。それによって各バーおよびグループの整数値を作成します

var groups = input.GroupBy(x => (int)((x.value - start)/width));

:私はあなたが好きな何かをする可能性がどのように一部

答えて

3

これは何か?

 Random randF = new Random(); 
     List<double> nums = new List<double>(); 
     for (int i = 0; i < 100000; i++) 
     { 
      nums.Add(randF.NextDouble()*100); 
     } 

     double fromXF = 30; 
     double toXF = 80; 
     int groupCount = 10; // number of groups between values 
     var histF = from i in nums 
        let groupKeyF = ((i-fromXF)/(toXF-fromXF)*groupCount) // even distribution of "groupCount" groups between fromXF and toXF, simple math, really 
        where groupKeyF >= 0 && groupKeyF < groupCount // only groups we want 
        let groupKey = (int)groupKeyF // clamp it to only group number 
        group i by groupKey into gr // group it according to group number 
        orderby gr.Key 
        select new { Value = gr.Key, Count = gr.Count() }; 

     foreach (var item in histF) 
     { 
      Console.WriteLine("Group number: " + item.Value + ", Count: " + item.Count); 
     } 
+0

これは良い解決策のようです。私はLINQにはそれほど素晴らしいことではありませんが、試してみる必要があります。ありがとう。 – James

+0

ゴールデン。 LINQの本当にきれいな使い方。ありがとう – James

3

してグループを達成するためには考えています。