I have the attached point list and use the following script to cluster the points. The clustering should just be applied to the first three-member of each sublist so I removed the fourth element from each sublist. However, after clustering the fourth member should be added to the associated sublist. How I can do that. https://pastebin.com/SKvFEEQD

A11 = Drop[dataDCMp, None, {4}];
nng = NearestNeighborGraph[A11, 4];
ccmp = ConnectedComponents[nng];
  • 1
    $\begingroup$ You could try something like NearestNeighborGraph[dataDCMp, 4, DistanceFunction -> (EuclideanDistance[#1[[;; 3]], #2[[;; 3]]] &)], which may take a while to finish. $\endgroup$
    – FJRA
    Jul 9 '20 at 2:16
  • $\begingroup$ Mehdi, can you try if Extract[dataDCMp, List /@ ConnectedComponents[IndexGraph@nng]] gives what you need? $\endgroup$
    – kglr
    Jul 9 '20 at 12:23

One way to do it, not efficient but overhead is small compared to generating the graph.

Import["https://pastebin.com/raw/SKvFEEQD"] // ToExpression;

(* Test on the first 100 to save time *)
assoc = <|Most@# -> Last@#|> & /@ dataDCMp[[1 ;; 100]];
A11 = assoc // Keys
nng = NearestNeighborGraph[A11, 4];
ccmp = ConnectedComponents[nng];

find[key_] := #[key] & /@ assoc // DeleteMissing

Map[Append[#, First@find[#]] &, ccmp, {-2}]
  • $\begingroup$ Thank you Rohit! Your code works but I am getting extra braces. I tried to remove them by flatten function but no proper answer. Do you have any idea what the solution is? $\endgroup$ Jul 9 '20 at 5:11
  • $\begingroup$ I found this "a //. {x_List} :> x " from former posts mathematica.stackexchange.com/questions/20180/… and it modified the braces. Thank you again, Rohit! $\endgroup$ Jul 9 '20 at 5:16
  • $\begingroup$ The analysis takes too long. Is there any way to do it faster? $\endgroup$ Jul 9 '20 at 6:53
  • $\begingroup$ @MehdiEbadi You mean time for NearestNeighborGraph? $\endgroup$ Jul 9 '20 at 17:31
  • $\begingroup$ Thank you Rohit! Now I realized that the speed is not too bad. I believe the problem was with my processor. $\endgroup$ Jul 9 '20 at 18:03

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.