2
$\begingroup$

Suppose I have got a list of numbers (say x-y coordinates)

a={{-99.107159975257815156, 0}, {-99.107159975199010630, 
  0}, {-99.107142118737087314, 0}, {-99.107142118737058893, 
  0}, {-99.107142118737058893, 0}, {-98.809552110008141289, 
  0}, {-95.833332619319349971, 0}, {-95.535743059443774445, 
  0}, {-95.238127643771221612, 0}, {-94.345237392311275941, 
  0}, {-93.067528042225021040, 0}, {-91.964285029098363129, 
  0}, {-91.666688282908694448, 0}, {-90.931371871528057200, 
  0}, {-90.608464933379565328, 0}, {-90.476189802090317444, 
  0}, {-90.476189802090317444, 0}, {-90.178570756688671413, 
  0}, {-89.583351887655183554, 0}, {-89.583332665885464525, 0}}

As we can see from the pattern that the points are very very close.

If I want to remove those points which are very close say by comparing the x and y coordinates of the consecutive coordinates, how will code. I have seen some functions like Select, Cases, DeleteDuplicatesand Sort will be useful, but the example are for single array of numbers.

For example

    b={-99.107159975257815156, -99.107159975199010630, \
    -99.107142118737087314, -98.809552110008141289, 95.833332619319349971};
Sort@DeleteDuplicates[b, Abs[#2 - #1] < 10^-2 &]

How can I proceed like this for list containing two arrays (Eg. list a given above)

$\endgroup$
  • $\begingroup$ Sort@DeleteDuplicates[a, Norm[#1 - #2, 2] < 10^-2 &]? You can play around with the second argument to norm, for 2 it is the usual Euclidean norm. $\endgroup$ – Kiro Feb 11 '18 at 12:27
3
$\begingroup$

If the list of points is very long, Nearest can help to find clusters more efficiently:

ϵ = 0.01
clusters = DeleteDuplicates[Sort /@ Nearest[a -> Automatic, a, {∞, ϵ}]];

Here, ϵ = 0.01 is the clustering radius; it is a parameter that you may adjust to your needs.

Afterwards you may create a new list containing only the first representatives of each cluster:

b = a[[clusters[[All, 1]]]]

{{-99.10715997525781516, 0}, {-98.80955211000814129, 0}, {-95.83333261931934997, 0}, {-95.53574305944377445, 0}, {-95.23812764377122161, 0}, {-94.34523739231127594, 0}, {-93.06752804222502104, 0}, {-91.96428502909836313, 0}, {-91.66668828290869445, 0}, {-90.931371871528057200, 0}, {-90.608464933379565328, 0}, {-90.47618980209031744, 0}, {-90.178570756688671413, 0}, {-89.58335188765518355, 0}}

Or you can create a new list containing the means of the clusters:

c = Compile[{{pts, _Real, 2}, {idx, _Integer, 1}},
   Mean[pts[[idx]]],
   RuntimeAttributes -> {Listable},
   Parallelization -> True
   ][a, clusters]

{{-99.1071, 0.}, {-98.8096, 0.}, {-95.8333, 0.}, {-95.5357, 0.}, {-95.2381, 0.}, {-94.3452, 0.}, {-93.0675, 0.}, {-91.9643, 0.}, {-91.6667, 0.}, {-90.9314, 0.}, {-90.6085, 0.}, {-90.4762, 0.}, {-90.1786, 0.}, {-89.5833, 0.}}

$\endgroup$

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.