MathGroup Archive 2011

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119909] Re: How to find the difference between two lists (opposite of union[]?)
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Wed, 29 Jun 2011 05:28:31 -0400 (EDT)

data = RandomInteger[{1, 1000}, 1000];

compl = Complement[r = Range[1000], Union[data]];

Union[data, compl] === r

True


Bob Hanlon

---- "McHale 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



  • Prev by Date: Re: Problem with Compile
  • Next by Date: Re: How to find the difference between two lists (opposite of union[]?)
  • Previous by thread: Re: How to find the difference between two lists (opposite of union[]?)
  • Next by thread: Re: How to find the difference between two lists (opposite of union[]?)