2
$\begingroup$

I want to gather some data together based on how close together they are in time. Consider this list of numbers:

{1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, 100, 101, 102, 103, 104, 105, 110, 111, 112}

We can see that there a subsets of different lengths, and within each subset the points are separated by some amount -- in this case $1$.

I want to be able to gather or sort the elements together based on some separation threshold. For the example list I gave the result would be:

{{1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15}, { 50, 51, 52, 53}, {100, 101, 102, 103, 104, 105}, {110, 111, 112}}

My intent is to be able to group datasets together which are close in time. Normally if all of my datasets have the same length I would use partition and be done with it, however my subsets have different lengths so it makes sense to group by how close together they are in time.

$\endgroup$
3
  • 1
    $\begingroup$ Split[{1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, 100, 101, 102, 103, 104, 105, 110, 111, 112}, Abs[#1 - #2] == 1 &] $\endgroup$ Mar 26, 2021 at 15:59
  • $\begingroup$ Nice, can this be extended to deal with $x-y$ data? But still only sorting/splitting by the x data? $\endgroup$
    – user27119
    Mar 26, 2021 at 16:08
  • $\begingroup$ Ah yes I think one can: Split[Data[[All, {1, 2}]], Abs[#1[[1]] - #2[[1]]] < 100 &] $\endgroup$
    – user27119
    Mar 26, 2021 at 16:13

1 Answer 1

4
$\begingroup$

Thanks to wuyudi's answer in the comments. One can sort simply by:

Split[Data[[All, {1, 2}]], Abs[#1[[1]] - #2[[1]]] < Thresh &]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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