Select duplicate points in the data list

I am trying to remove some data list items in Mathematica, but I do not understand how Select and Union . 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 elements in (0,1)x(0,1) are needed, given that the points in the radius of the distance 0.05 are duplicates. In this example

 list1 ={{0.10,0.20},{0.70,0.80}} 

I don't care which element represents the equivalence class. Im doing the following:

 list1 = Select[list, 0 < Part[#,1] <1 &];Select[list,0 < Part[#,2] <1 &] 

which gives points in (0,1)x(0,1) , but if I try to use Union , for example

 Union[list1, SameTest -> (Abs[#1-#2] < 0.05 &)] 

I get errors in slots.

Can someone explain to me how to do this 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.

+4
source share
1 answer

There are several ways to approach this. One way to do this, I think, is the most neat, since you need your items to be at regular intervals, it is to use IntervalMemberQ with Select .

For example, to narrow the 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 an ideal 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}} 
+4
source

Source: https://habr.com/ru/post/1379933/


All Articles