Re: How to find the difference between two lists (opposite of union[]?)

*To*: mathgroup at smc.vnet.net*Subject*: [mg119913] Re: How to find the difference between two lists (opposite of union[]?)*From*: Leo Ditolaghi <leoditolaghi at gmail.com>*Date*: Wed, 29 Jun 2011 05:29:15 -0400 (EDT)

Hi Paul, Well, I'm still a Mathematica noob, but a walk around the help docs gave me this solution, which seems fairly straightforward (I used the range 1 to 10 for illustration): (* numbers from 1 to 10 not represented in a randomly generated list of integers of the same range *) randomNumbers = RandomInteger[{1,10},10] Out[6]= {9,4,1,6,5,8,4,9,5,8} In[7]:= sequence = Range[10] Out[7]= {1,2,3,4,5,6,7,8,9,10} In[8]:= Complement[sequence,randomNumbers] Out[8]= {2,3,7,10} Cheers, Leo On Tue, Jun 28, 2011 at 7:55 AM, McHale, Paul <Paul.McHale at excelitas.com>wrote: > I have been working through a neat book called Mathematica(r): A > Problem-Centered Approach< > http://www.amazon.com/Mathematica%C2%AE-Problem-Centered-Approach-Undergraduate-Mathematics/dp/1849962502/ref=sr_1_2?ie=UTF8&qid=1309181549&sr=8-2>. > I have a problem I would like to know if there is a more elegant solution. > I would think involving set theory, but I can't seem to (embarrassingly) > find it. > > I want to generate 1000 random integers between 1 and 1000. Then find out > what numbers between 1 and 1000 were not generated. > > (* Generate random list of 1000 numbers with range 1-1000*) > mList=RandomInteger[{1,1000},1000] //Sort; > > (* Find out what numbers were omitted in the range 1-1000*) > (* Make a list of numbers 1-1000 with position of each within original > array *) > mList=Table[{i,Position[mList,i] //Flatten},{i,1,1000}]; > > (* select numbers with no position in original array (musical chairs) *) > Select[mList,Length[#[[2]]]==0&][[All,1]] > > This works creating a lot of extra array in the process. Just wanted to > ask what are the methods you guys would use to solve this? > > A funny (non-working) way of stating the same thing would be: > > (* Generate random list of 1000 numbers with range 1-1000*) > mList=RandomInteger[{1,1000},1000] //Sort; > > Solve[ Join[mList,mDiff]==Range[1000],mDiff] > > Thanks, > Paul > > > Paul McHale | Electrical Engineer, Energetics Systems | Excelitas > Technologies Corp. > > Phone: +1 937.865.3004 | Fax: +1 937.865.5170 | Mobile: +1 > 937.371.2828 > 1100 Vanguard Blvd, Miamisburg, Ohio 45342-0312 USA > Paul.McHale at Excelitas.com<mailto:Paul.McHale at perkinelmer.com> > www.excelitas.com<http://www.excelitas.com> > > [cid:image001.png at 01CB9136.E3D96D90] > > Please consider the environment before printing this e-mail. > This email message and any attachments are confidential and proprietary to > Excelitas Technologies Corp. If you are not the intended recipient of this > message, please inform the sender by replying to this email or sending a > message to the sender and destroy the message and any attachments. > Thank you >