MathGroup Archive 2006

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

Search the Archive

Re: programming problem about elements taken


Barrow schrieb:
> 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
> 

Hi Barrow,

the first method coming to my mind has been the use of ReplaceRepeated, but
for long lists this lasts far too long:


A=Table[Random[],{2000}];
epsilon=1.*^-4;

Timing[Length[
    r1=A//.{x___,c_,y___}/;Min[Abs[c-{x,y}]]<epsilon:>{x,y}
    ]]

--> {22.125 Second,1650}

It is sufficient to compare element k with elements k+1,...,n:

selectDistinguishable[a_List,eps_]:=
  Pick[a,Min[Abs[a[[#]]-Drop[a,#]]]>eps&/@Range[Length[a]]]

Timing[Length[
    r2=selectDistinguishable[A,epsilon]
    ]]

--> {0.11 Second,1650}

_Much_ better :-)

r1===r2
--> True

hth,
Peter


  • Prev by Date: Troubleshooting Mathematic's Help
  • Next by Date: Re: programming problem about elements taken
  • Previous by thread: Re: programming problem about elements taken
  • Next by thread: Re: programming problem about elements taken