MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Extension to BinLists Function


The documentation shows examples of BinLists putting into
bins one dimensional vectors of numbers such as the following 

data = {1,3,2,1,4,5,6,2};
breakPoints =  {-Infinity,2,5,7,Infinity}; 

BinLists[data, {breakPoints}]

which returns: 

{{1, 1}, {3, 2, 4, 2}, {5, 6}, {}}

I would like to put into bins entire sublists of data
of arbitray  depth  such as the following 
example where every sublist is 2-dimensional:

data1 = Transpose[{data, Table[Random[],{Length[data]}]}]

which results for the values of data1:  


In this simple example, the sublists are binned based on the value of the first element 
of every sublist.  

The result, using the same breakpoints (this time applied to the first
element of every sublist as in the example above),
should be:


The binLists function below does this job.
But, it uses brute force in the form of a couple of
nested For functions to accomplish this.  
Is there a more efficient way  of binning
sublists of arbitrary depth?

Thank you.



For the second example above, which uses the 
binLists function defined below, the inputs to the  binLists
function  are:

array = data1
breakPts = {2, 5, 7}
pos = {1}

binLists[data1, breakPts, pos]



which is the correct result.


Definition of binLists: 

 Remove[binLists ];
 binLists[array_List, breakPts_List, pos_List:{}  ] := 

  breakPtIntervalV= Partition[Join[{-Infinity},breakPts,{Infinity}], 2, 1];
  nIntervals = Length[breakPtIntervalV];
  bins = Table[{},{nIntervals}];
  elemV holds the element from each sublist in array that
     that binning is to be a function of
  If[Length[pos] > 0,
        elemV = #[[Apply[Sequence, pos]]]& /@ array,
        elemV = array
    ];(* If Length *)
  For[j = 1, j<= Length[array], ++j,
      For[k=1, k<=nIntervals, ++k,
         elemV[[j]] >= breakPtIntervalV[[k,1]] &&
         elemV[[j]] < breakPtIntervalV[[k,2]],
            AppendTo[bins[[k]], array[[j]]]
        ];(* For k *)
  ];(* For j *)
  ](* End Module binLists  *)

  • Prev by Date: Re: Rule replacement doesn't work after NDSolve?
  • Next by Date: Re: Rule replacement doesn't work after NDSolve?
  • Previous by thread: Re: NDSolve and "periodic" boundary conditions
  • Next by thread: Re: Extension to BinLists Function