Let's say we have two tables that have entries with arbitrary decimals. I joined them using Join and now I want to use DeleteDuplicates to delete the duplicates which have the same numbers till 5 decimal points. How can I do that?
$\begingroup$
$\endgroup$
2
-
1$\begingroup$ How can I do that? could you provide an example to use? One way might be to use Round on the numbers first to round them to 5 decimal points, then use DeleteDuplicates after that? But need an example of your input to try this on first. $\endgroup$– NasserOct 23, 2022 at 14:42
-
$\begingroup$ Or instead of Round, may be Chop is what is needed. Chop takes second argument delta which can be used for this. $\endgroup$– NasserOct 23, 2022 at 14:49
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
$Version
(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)
Clear["Global`*"]
data = RandomReal[{0, 0.0001}, 100];
DeleteDuplicates[data, Abs[#1 - #2] < 10^-5 &]
(* {0.0000447438, 0.0000868851,
8.09452*10^-6, 0.0000764421, 0.0000279434, 0.0000593133,
0.000098101} *)
Union[data, SameTest -> (Abs[#1 - #2] < 10^-5 &)]
(* {1.91036*10^-6, 0.0000130352, 0.0000230656, 0.0000343098,
0.0000447438, 0.0000552897, 0.0000690272, 0.0000792503,
0.0000893103} *)
DeleteDuplicates[Round[data, 10.^-5]]
(* {0.00004, 0.00009, 0.00001, 0.00008, 0.00007, 0.00005, 0.00003,
0.00006, 0.0001, 0.00002, 0.} *)
Union[Round[data, 10.^-5]]
(* {0., 0.00001, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006,
0.00007, 0.00008, 0.00009, 0.0001} *)