I would like to know how I can divide the attached list of points to as many clusters as possible based on the gap between the patches. Then I can decide which one should be kept and which remove. I tried different options in Findclusters command but none of them gave me a correct answer. Any help is really appreciated.


enter image description here


This gets you four clusters with points grouped together in ccmp. The two large islands are depicted in orange and blue, the small long island in green, and a tiny island in red.

nng = NearestNeighborGraph[data, 8];
ccmp = ConnectedComponents[nng];

(* Length@ccmp == 4 *)

connected components list plot

  • $\begingroup$ Thank you! It worked! However, when I changed the value of the coefficient in the bracket of the NeasrestNeighborGraph from 8 to 4, the number of patches changed to 5 and showed me another small island as a separate cluster which is correct basically. Is there any criterion to select the value of the coefficient? For example, the value of 1 gave me 30 patches. I am asking as I need to apply the script on different data and need to use a constant coefficient. $\endgroup$ – Mehdi Ebadi Jun 13 '20 at 17:27
  • $\begingroup$ I just chose 8 randomly. If instead you put a list {k,r} it uses the k nearest within distance r, and {All,r} connects all elements within distance r $\endgroup$ – flinty Jun 13 '20 at 17:31
  • $\begingroup$ Thank you for the response! By distance, you mean Euclideandistance? $\endgroup$ – Mehdi Ebadi Jun 13 '20 at 17:38
  • $\begingroup$ Yes by default, but it also takes an option DistanceFunction, e.g , DistanceFunction -> ManhattanDistance $\endgroup$ – flinty Jun 13 '20 at 17:43
  • $\begingroup$ Another question and appreciate your response. Is always the output from the largest list (with the most number of members) to the smallest one? $\endgroup$ – Mehdi Ebadi Jun 13 '20 at 18:29

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.