Re: Generalizing Complement to handle multiple occurrences
- To: mathgroup at smc.vnet.net
- Subject: [mg112480] Re: Generalizing Complement to handle multiple occurrences
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Fri, 17 Sep 2010 06:39:49 -0400 (EDT)
Christoph, This is only so because your ranges for random integers are so much smaller than the number of generated integers. After Union in your code is done, you are left with at most 41 numbers, which is a small enough list so that Alternatives can do a pretty good job to hide the true Length[a]*Length[b] complexity of this approach. Try this: a = RandomInteger[{1, 100000}, 1000000]; b = RandomInteger[{30, 70000}, 1000000]; I was not patient enough to see it finished. Of course, that matters mostly for the second list, in this method. Regards, Leonid On Thu, Sep 16, 2010 at 1:58 PM, Christoph Lhotka < christoph.lhotka at univie.ac.at> wrote: > Hello, > > for 1 000 000 random elements on my computer a factor 3 slower than the > built in Complement function: > > a = RandomInteger[{1, 100}, 1000000]; > b = RandomInteger[{30, 70}, 1000000]; > > DeleteCases[a, Alternatives@@Union[b]] > > Ciao, > > Christoph > > On 15/09/2010 10:38, Mark Coleman wrote: > > Greetings, > > > > I'm wondering how one can efficiently generalize the built-in > > Complement function to return multiple occurrences of elements. For > > instance, the current version generates > > > > a={1,2,2,3,4,4,4,4} > > b={3,5,7} > > > > Complement[a,b]={1,2,4} > > > > I'm looking for a way to define > > > > myComplement[a,b]={1,2,2,4,4,4,4} > > > > My current code is very inefficient! > > > > Thanks, > > > > Mark > > > > > >