Re: Generalizing Complement to handle multiple occurrences of elements
- To: mathgroup at smc.vnet.net
- Subject: [mg112466] Re: Generalizing Complement to handle multiple occurrences of elements
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 16 Sep 2010 06:01:11 -0400 (EDT)
On 9/15/10 at 4:38 AM, markspcoleman at gmail.com (Mark Coleman) wrote: >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} Here are a few ways: In[3]:= c = Intersection[a, b]; DeleteCases[a, _?(MemberQ[c, #] &)] Out[4]= {1,2,2,4,4,4,4} In[5]:= c = Complement[a, b]; Cases[a, _?(MemberQ[c, #] &)] Out[6]= {1,2,2,4,4,4,4} In[8]:= Select[a, MemberQ[c, #] &] Out[8]= {1,2,2,4,4,4,4}