2
$\begingroup$

I am just curious that there is any better way to write the code to search the nearest value than what I wrote below

peaks={{127, 1}, {133, 0.992}, {139, 1}, {1762, 0.984}};

DeleteDuplicates[
Table[
  Select[peaks,
   (peaks[[i, 2]] - 0.1 <= #[[2]] <= peaks[[i, 2]] + 0.1) &&
     (peaks[[i, 1]] - 20 <= #[[1]] <= peaks[[i, 1]] + 20)
    &]
  , {i, 1, Length[peaks]}]]

(*Output*)
{{{127, 1}, {133, 0.992}, {139, 1}}, {{1762, 0.984}}}

I think there will be much simpler and easier way to write in mathematica... Any help?


Edit

I try to gather the value by the near position

enter image description here

You can see that three points are All in blue point, and one point is at yellow dot. I just want to find this cluster

$\endgroup$
3
  • $\begingroup$ Nearest. But please describe what you want the code to do. $\endgroup$
    – corey979
    Oct 4 '16 at 15:30
  • 1
    $\begingroup$ A slight tweak to your code: Gather[peaks, And[Abs[#1[[1]] - #2[[1]]] < 20, Abs[#1[[2]] - #2[[2]]] < 0.1] &], assuming I understand your question. But I echo @corey979: Can you explain what it is you're trying to do? $\endgroup$
    – march
    Oct 4 '16 at 15:32
  • $\begingroup$ Thank you for the comment, I add the edit below $\endgroup$
    – Saesun Kim
    Oct 4 '16 at 15:44
4
$\begingroup$
peaks={{127, 1}, {133, 0.992}, {139, 1}, {1762, 0.984}};
  1. FindClusters[peaks, Method -> "Agglomerate"]

    {{{127, 1}, {133, 0.992}, {139, 1}}, {{1762, 0.984}}}

  2. cc = ClusterClassify[peaks, Method -> "DBSCAN"]; GatherBy[peaks, cc]

    {{{127, 1}, {133, 0.992}, {139, 1}}, {{1762, 0.984}}}

$\endgroup$
0
3
$\begingroup$

This should be exactly reproducing the OP code, using Nearest :

 Sort /@ (Nearest[peaks, #, {Infinity, 1.},
      DistanceFunction -> (Max[Abs[Subtract@##/{20, .1}]] &)
           ] & /@ peaks) // DeleteDuplicates

and you might also consider DistanceFunction -> (Norm[Subtract@##/{20, .1}] &)

$\endgroup$
3
  • $\begingroup$ DistanceFunction -> EuclideanDistance? $\endgroup$
    – yode
    Oct 4 '16 at 17:36
  • $\begingroup$ @yode you could also use EuclideanDistance but you still need to apply the scale {20,.1} , so like EuclideanDistance[#1/{20, .1}, #2/{20, .1}] & $\endgroup$
    – george2079
    Oct 4 '16 at 17:42
  • $\begingroup$ @george2079 I think this code was what I asked on question, but sorry that I did not accept your post. $\endgroup$
    – Saesun Kim
    Oct 4 '16 at 19:21

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.