MathGroup Archive 2010

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

Search the Archive

Re: Generalizing Complement to handle multiple occurrences of elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112455] Re: Generalizing Complement to handle multiple occurrences of elements
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Thu, 16 Sep 2010 05:59:09 -0400 (EDT)
  • References: <i6q0ml$l7c$1@smc.vnet.net>

My brute force approach:

myComplement[a_, b___] := Cases[a, _?(MemberQ[Complement[a, b], #] &)]

Don't know about efficiency. With the first list containing 10,000
integers it takes 22 sec on my slow laptop.

Cheers -- Sjoerd

On Sep 15, 10:38 am, Mark Coleman <markspcole... at gmail.com> 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



  • Prev by Date: Re: Generalizing Complement to handle multiple occurrences of elements
  • Next by Date: Re: Why Row does not format 2 plots in one row when ImageSize->Full?
  • Previous by thread: Re: Generalizing Complement to handle multiple occurrences of elements
  • Next by thread: Re: Generalizing Complement to handle multiple occurrences of elements