Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #3386

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

From Bob Hanlon <hanlonr@cox.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to find the difference between two lists (opposite of union[]?)
Date 2011-06-29 21:46 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iug6eq$9k6$1@smc.vnet.net> (permalink)

Show all headers | View raw


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@Excelitas.com<mailto:Paul.McHale@perkinelmer.com>
www.excelitas.com<http://www.excelitas.com>

[cid:image001.png@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

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: How to find the difference between two lists (opposite of union[]?) Bob Hanlon <hanlonr@cox.net> - 2011-06-29 21:46 +0000

csiph-web