Re: Dropping Similar Numbers from List
- To: mathgroup at smc.vnet.net
- Subject: [mg60577] Re: [mg60564] Dropping Similar Numbers from List
- From: "Owen, HL \(Hywel\)" <h.l.owen at dl.ac.uk>
- Date: Tue, 20 Sep 2005 06:16:20 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
You're right that SetAccuracy and SetPrecision don't have the desired
effect, e.g.
mylist = {1.234, 1.234, 1.234, 1.235, 1.235, 1.240}
Union[mylist]
{1.234, 1.235, 1.24}
SetAccuracy[mylist, 3]
{1.23, 1.23, 1.23, 1.24, 1.24, 1.24}
SetAccuracy[mylist, 3] // Union
{1.23, 1.24, 1.24}
Instead I wrote a function RoundOff which does what you want:
RoundOff[n_, len_] := N[FromDigits[RealDigits[n, 10, len]]]
Union[RoundOff[#, 3] & /@ mylist]
{1.23, 1.24}
There's probably better ways to do this. You can probably also make
RoundOff thread better over lists.
-------------------------------------------
Hywel Owen h.owen at dl.ac.uk
Accelerator Science and Technology Centre
CCLRC Daresbury Laboratory
United Kingdom
Tel/Fax: +44 1925 603797/603192
-------------------------------------------
"Errors using inadequate data are much less than those using no data at
all."
-- Charles Babbage
> -----Original Message-----
> From: andre [mailto:erasmus.the.giant at gmail.com]
To: mathgroup at smc.vnet.net
> Sent: 20 September 2005 10:20
> Subject: [mg60577] [mg60564] Dropping Similar Numbers from List
>
> My data set has some numbers which differ by a small amount,
> say one part in 10^-15, that I don't want to appear more than
> once. If they were identical, I would just use Union[data]
> and be finished. Is there a way to set a tollerance for the
> Union function?
>
> I tried to use SetPrecision to get rid of the unwanted extra
> digits, but Union[SetPrecision[data,n]] gives the same result
> as Union[data].
> What exactly does SetPrecision do? It seems that it's not
> just dropping the digits beyond the limit I set.
>
> Andre
>
>
>