1
$\begingroup$

I have an issue with the following code. I am trying to remove the points which have a distance less than 2mm relative to each other starting with a specified point frameCenter1. The weird part is that in the second for loop when I apply counter11 <= Length[b1[[All, 1]]] as the test, it gives me the correct answer but when I run the Bhn2 = Length[b1[[All, 1]]] or Length[b1] earlier and substitute the value Bhn2 as the test (i.e counter11 <= Bhn2) it gives me wrong answer with the following message Part 7 of {{7,7},{8,5},{8,7},{7,6}} does not exist. This should not happen as they are the same. I just substitute the value of the test (i.e Bhn2) instead of Length[b1[[All, 1]]].

dalist = {{9, 6}, {5, 6}, {6, 0}, {0, 5}, {10, 8}, {1, 2}, {10, 
4}, {1, 1}, {7, 7}, {6, 8}, {5, 3}, {6, 9}, {7, 4}, {1, 8}, {10, 
0}, {10, 7}, {6, 3}, {4, 0}, {9, 2}, {4, 7}, {1, 6}, {10, 8}, {7, 
8}, {0, 9}, {3, 4}, {0, 0}, {8, 5}, {4, 5}, {6, 0}, {2, 9}, {2, 
4}, {8, 4}, {7, 4}, {3, 6}, {7, 9}, {1, 9}, {1, 4}, {8, 0}, {8, 
9}, {5, 4}, {2, 5}, {2, 9}, {3, 1}, {0, 6}, {10, 3}, {9, 6}, {8, 
7}, {7, 6}, {7, 3}, {8, 9}, {7.5, 9}, {6.5, 9}, {7, 9}, {1, 
5}, {2, 6}, {1, 10}, {0.5, 8}, {1.5, 8}, {0.5, 7}, {1.5, 7}, {0.5,
 6}, {1.5, 6}, {0.5, 5}, {1.5, 5}, {0.5, 4}, {1.5, 4}, {0.5, 
9}, {1.5, 9}, {1, 7}, {2, 8}, {7, 10}, {9, 4}, {8, 4}, {8, 3}, {9,
 5}, {9, 3}, {7.5, 3}, {8.5, 3}, {9.5, 4}, {8.5, 4}, {9.5, 
4}, {7.5, 4}, {9.5, 4}}; 
Print[ListPlot[{dalist[[All, 1 ;; 2]]}, PlotStyle -> {Red}]]
Clear[b1, G1, S1, dataPnut2, Bhn, Bhn2, counter, counter1]
frameCenter1 = {{1, 10}, {7, 10}, {9, 4}}
Bhn = Length[frameCenter1]
counter0 = 1
For[counter = counter0, counter <= Bhn, counter++,
criticalRadius1 = 2;
b1 = Select[dalist, 
EuclideanDistance[#, frameCenter1[[counter]]] < criticalRadius1 &];
dataPnut2 = DeleteCases[dalist, Alternatives @@ b1];
Print[ListPlot[{dataPnut2[[All, 1 ;; 2]]}, PlotStyle -> {Blue}]];

 G1 = b1;
 Bhn2 = Length[b1];
 d1 = {};
 counter1 = 1;
 For[counter11 = counter1, counter11 <= Length[b1[[All, 1]]], 
  counter11++,
  b1 = Select[dataPnut2, 
    EuclideanDistance[#, b1[[counter11]]] < criticalRadius1 &];
  d1 = Join[d1, b1];
  ];
 S1 = Join[G1, d1];
 dataPnut2 = DeleteCases[dalist, Alternatives @@ S1];
 b1 = d1;
 dalist = dataPnut2;
 Print[ListPlot[{dataPnut2[[All, 1 ;; 2]]}, PlotStyle -> {Green}]]
 ]
$\endgroup$
  • $\begingroup$ Please fix the syntax errors in your code. $\endgroup$ – Rohit Namjoshi Jun 7 at 17:47
  • $\begingroup$ But I do not get syntax error in this code otherwise that I apply the aforementioned change in the question. (i.e run Bhn2 = Length[b1[[All, 1]]] or Length[b1] earlier and substitute the value Bhn2 as the test (i.e counter11 <= Bhn2) in the 2nd for loop) $\endgroup$ – Mehdi Ebadi Jun 7 at 17:53
  • $\begingroup$ My question is why applying that change give the wrong result while they are the same. $\endgroup$ – Mehdi Ebadi Jun 7 at 18:00
  • $\begingroup$ Thank you! modified. $\endgroup$ – Mehdi Ebadi Jun 7 at 20:03
3
$\begingroup$

Update

To filter multiple frameCenter

filter[list_, criticalRadius_, frameCenter_] := 
 Select[list, EuclideanDistance[#, frameCenter] >= criticalRadius &]

frameCenters = {{1, 10}, {7, 10}, {9, 4}};
filter[dalist, criticalRadius, #] & /@ frameCenters

Is there are reason for wanting to do this procedurally? A functional solution is much shorter and easier to understand.

To remove all points that are closer than some distance from a given point you could do this.

frameCenter = {1, 10};
criticalRadius = 2;

Select[dalist, EuclideanDistance[#, frameCenter] >= criticalRadius &]

For multiple frameCenter values, just wrap the above in a function and Map over the list of frameCenter values.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you! It is just part of my routine. Could you please explain how I can wrap it in a function. Can I define select as a function? Select[dalist, EuclideanDistance[#, frameCenter1] >= 2 &] & /@ {{1, 10}, {7, 10}, {9, 4}} I am confused as the variables are fed from framecenter1 and dalist. $\endgroup$ – Mehdi Ebadi Jun 8 at 0:46
  • $\begingroup$ @MehdiEbadi Updated answer to show how to wrap the Select in a function and Map. $\endgroup$ – Rohit Namjoshi Jun 8 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.