Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7610 > unrolled thread
| Started by | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| First post | 2012-01-06 09:21 +0000 |
| Last post | 2012-01-06 09:21 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Extension to BinLists Function Bob Hanlon <hanlonr357@gmail.com> - 2012-01-06 09:21 +0000
| From | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| Date | 2012-01-06 09:21 +0000 |
| Subject | Re: Extension to BinLists Function |
| Message-ID | <je6ee4$qag$1@smc.vnet.net> |
breakPoints = {-Infinity, 2, 5, 7, Infinity};
data1 = {{1, 0.936229}, {3, 0.128096}, {2, 0.393583}, {1, 0.301525},
{4, 0.503822}, {5, 0.253597}, {6, 0.0835316}, {2, 0.0068356}};
As stated in the documentation, BinLists handles multi-dimensional data
First /@ BinLists[data1, {breakPoints}, {{-Infinity, Infinity}}]
{{{1, 0.936229}, {1, 0.301525}}, {{3, 0.128096}, {2, 0.393583}, {4,
0.503822}, {2, 0.0068356}}, {{5, 0.253597}, {6, 0.0835316}}, {}}
Bob Hanlon
On Thu, Jan 5, 2012 at 5:57 AM, Don <donabc@comcast.net> wrote:
> Hello,
>
> The documentation shows examples of BinLists putting into
> bins one dimensional vectors of numbers such as the following
> example:
>
> 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:
>
> {{1,0.936229},{3,0.128096},{2,0.393583},{1,0.301525},{4,0.503822},{5,0.253597},{6,0.0835316},{2,0.0068356}}
>
> 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:
>
>
> {{{1,0.936229},{1,0.301525}},{{3,0.128096},{2,0.393583},{4,0.503822},{2,0.0068356}},{{5,0.253597},{6,0.0835316}},{}}
>
>
> 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.
>
> Don
>
> =========================
>
> 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]
>
> returns
>
> {{{1,0.936229},{1,0.301525}},{{3,0.128096},{2,0.393583},{4,0.503822},{2,0.0068356}},{{5,0.253597},{6,0.0835316}},{}}
>
> which is the correct result.
>
> =========================
>
> Definition of binLists:
>
> Remove[binLists ];
>
> binLists[array_List, breakPts_List, pos_List:{} ] :=
> Module[{},
>
>
>
> 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,
>
> If[
> elemV[[j]] >= breakPtIntervalV[[k,1]] &&
> elemV[[j]] < breakPtIntervalV[[k,2]],
> AppendTo[bins[[k]], array[[j]]]
> Continue[]
> ]
>
>
> ];(* For k *)
>
> ];(* For j *)
>
> Return[bins]
>
> ](* End Module binLists *)
>
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web