Re: programming problem about elements taken (CORRECTION)
- To: mathgroup at smc.vnet.net
- Subject: [mg72479] Re: [mg72470] programming problem about elements taken (CORRECTION)
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sun, 31 Dec 2006 05:27:05 -0500 (EST)
- Reply-to: hanlonr at cox.net
The second function should have been written as choose2[a_?VectorQ, epsilon_?NumericQ] := Module[ {sa = Sort[a], sel}, sel = Fold[If[#2 - Last[#1] > epsilon, Append[#1, #2], #1] &, {First[sa]}, Rest[sa]]; Select[a, MemberQ[sel, #] &]]; Still much slower than choose1 but much better than the original choose2. Bob Hanlon ---- Bob Hanlon <hanlonr at cox.net> wrote: > For a sorted output: > > choose1[a_?VectorQ, epsilon_?NumericQ]:=Module[ > {sa=Sort[a]}, > Fold[If[#2-Last[#1]>epsilon, > Append[#1,#2],#1]&,{First[sa]},Rest[sa]]]; > > To retain the original element order: > > choose2[a_?VectorQ, epsilon_?NumericQ]:=Module[ > {sa=Sort[a]}, > Select[a,MemberQ[Fold[If[#2-Last[#1]>epsilon, > Append[#1,#2],#1]&,{First[sa]},Rest[sa]], #]&]]; > > a=Table[Random[],{20}] > > {0.67319,0.378445,0.981498,0.579474,0.798173,0.537112,0.884835,0.534093,0.\ > 0471484,0.0516117,0.988746,0.570592,0.261828,0.632062,0.754747,0.436792,0.\ > 669037,0.149373,0.802271,0.484141} > > choose1[a,0.1] > > {0.0471484,0.149373,0.261828,0.378445,0.484141,0.632062,0.754747,0.884835,0.\ > 988746} > > choose2[a,0.1] > > {0.378445,0.884835,0.0471484,0.988746,0.261828,0.632062,0.754747,0.149373,0.\ > 484141} > > %%==Sort[%] > > True > > However, the second method (original order) is much slower for a large list. > > a=Table[Random[],{1000}]; > > Timing[choose1[a,0.1];] > > {0.008364 Second,Null} > > Timing[choose2[a,0.1];] > > {1.23431 Second,Null} > > > Bob Hanlon > > ---- Barrow <GRseminar at gmail.com> wrote: > > Dear all, > > I have a list of numbers (A),in fact, they are numbers distributed > > over [0,1]. Given parameter \epsilon, I have to choose elements from A > > such that their distances are larger than \epsilon, so the elements are > > "distringuishable". My goal is to find the maximum number of elements > > from A to meet the above "distinct criterion". > > How to do it with the functions build-in in mathematica ?? > > Thanks in advence. Sincerely Barrow > >