I'm trying to drop some elements of a list of data in Mathematica, but I don't understand how Select
and Union
work. For example, suppose I have the following list
list = {{0.10,0.20},{1.10,0.20},{0.70,0.80},{0.20,1.10},
{1.20,1.20},{0.12,0.18},{0.68,0.76}}
and only want elements in (0,1)x(0,1)
considering that points within a distance radius of 0.05
are duplicates. In the example,
list1 ={{0.10,0.20},{0.70,0.80}}
I don't care which element represents the equivalence class. I'm doing the following:
list1 = Select[list, 0 < Part[#,1] <1 &];Select[list,0 < Part[#,2] <1 &]
which gives the points in (0,1)x(0,1)
, but if I try to use Union
, such as
Union[list1, SameTest -> (Abs[#1-#2] < 0.05 &)]
I get slot errors.
Can someone explain to me how to do it neatly?
---EDIT---
Using
DeleteDuplicates[list1, Abs[Part[#1, 1] - Part[#2, 1]] < 10^-6 &]
does the trick, but I wonder why I can't work with a list of lists.
There are several ways of approaching this. One way to do it, that I think is the neatest, since you require your elements to be in certain intervals, is to use IntervalMemberQ
with Select
.
For example, to narrow down your list to those points in (0,1)x(0,1)
:
list01 = Select[list, And @@ IntervalMemberQ[Interval[{0, 1}], #] &]
Out[1]= {{0.1, 0.2}, {0.7, 0.8}, {0.12, 0.18}, {0.68, 0.76}}
Secondly, to eliminate duplicates, use DeleteDuplicates
, which is the perfect tool for this task. You can use the same test using IntervalMemberQ
:
DeleteDuplicates[list01,
And @@ IntervalMemberQ[Interval[{0, 0.5}], Abs[#1 - #2]] &]
Out[2]= {{0.1, 0.2}, {0.7, 0.8}}