3
$\begingroup$

For example I have a ordered list

l=Sort@Join[Range[-10,10,0.01],Range[-20,-15,0.05],Range[20,23,0.2]]

And I want to split the data according to the "apparent" large gaps in data, so the expected output would be

{Range[-20,-15,0.05],Range[-10,10,0.01],Range[20,23,0.2]}

Example of real data

{2.06764, -2.06764, -2.06757, 2.06757, -2.06745, 2.06745, -2.06728, \
2.06728, -2.06707, 2.06707, 2.06681, -2.06681, -2.0665, 2.0665, \
-2.06615, 2.06615, -2.06576, 2.06576, -2.06533, 2.06533, -2.06486, \
2.06486, -2.06436, 2.06436, -2.06382, 2.06382, -2.06325, 2.06325, \
2.06265, -2.06265, 2.06203, -2.06203, -2.06138, 2.06138, -2.06072, \
2.06072, -2.06004, 2.06004, -2.05935, 2.05935, -2.05866, 2.05866, \
2.05796, -2.05796, 2.05726, -2.05726, -2.05656, 2.05656, -2.05588, \
2.05588, -2.05522, 2.05522, -2.05457, 2.05457, -2.05395, 2.05395, \
-2.05335, 2.05335, -2.05279, 2.05279, -2.05227, 2.05227, -2.05179, \
2.05179, -2.05136, 2.05136, -2.05097, 2.05097, -2.05064, 2.05064, \
2.05037, -2.05037, -2.05015, 2.05015, 2.05, -2.05, 2.0499, -2.0499, \
1.77731, -1.77731, -1.77731, 1.77731, -1.75793, 1.75793, 1.75748, \
-1.75748, 1.75675, -1.75675, 1.75572, -1.75572, 1.75442, -1.75442, \
-1.75283, 1.75283, -1.75098, 1.75098, 1.74887, -1.74887, -1.74649, \
1.74649, 1.74387, -1.74387, -1.74101, 1.74101, -1.73792, 1.73792, \
1.7346, -1.7346, -1.73106, 1.73106, -1.72731, 1.72731, 1.72336, \
-1.72336, 1.71922, -1.71922, 1.71488, -1.71488, 1.71037, -1.71037, \
1.70568, -1.70568, 1.70084, -1.70084, -1.69584, 1.69584, 1.69071, \
-1.69071, 1.68544, -1.68544, 1.68007, -1.68007, -1.67459, 1.67459, \
-1.66903, 1.66903, -1.66342, 1.66342, 1.65777, -1.65777, 1.65213, \
-1.65213, -1.64654, 1.64654, -1.64105, 1.64105, -1.63574, 1.63574, \
-1.63071, 1.63071, -1.62606, 1.62606, 1.62194, -1.62194, -1.61852, \
1.61852, -1.61594, 1.61594, 1.61435, -1.61435, 1.52612, -1.52612, \
1.52389, -1.52389, 1.52036, -1.52036, 1.51572, -1.51572, -1.51019, \
1.51019, 1.50394, -1.50394, -1.49711, 1.49711, -1.48981, 1.48981, \
1.48213, -1.48213, 1.47412, -1.47412, 1.46583, -1.46583, 1.4573, \
-1.4573, -1.44856, 1.44856, 1.43963, -1.43963, 1.43052, -1.43052, \
1.42125, -1.42125, -1.41183, 1.41183, 1.40227, -1.40227, -1.39258, \
1.39258, 1.38277, -1.38277, 1.37284, -1.37284, 1.36279, -1.36279, \
-1.35264, 1.35264, -1.34239, 1.34239, 1.33204, -1.33204, -1.32159, \
1.32159, -1.31105, 1.31105, -1.30043, 1.30043, 1.28973, -1.28973, \
1.27895, -1.27895, -1.2681, 1.2681, 1.25719, -1.25719, 1.24623, \
-1.24623, 1.23523, -1.23523, 1.22422, -1.22422, -1.21325, 1.21325, \
-1.20242, 1.20242, -1.192, 1.192, 1.18287, -1.18287, -1.1521, 1.1521, \
-1.15034, 1.15034, -1.13894, 1.13894, -1.12824, 1.12824, -1.117, \
1.117, 1.1055, -1.1055, -1.09384, 1.09384, -1.08208, 1.08208, \
1.07025, -1.07025, -1.05835, 1.05835, -1.04641, 1.04641, 1.03443, \
-1.03443, -1.02242, 1.02242, -1.0104, 1.0104, 0.998357, -0.998357, \
-0.986315, 0.986315, -0.974278, 0.974278, 0.962258, -0.962258, \
0.950264, -0.950264, -0.938309, 0.938309, 0.926406, -0.926406, \
0.91457, -0.91457, 0.90282, -0.90282, -0.891177, 0.891177, 0.879662, \
-0.879662, 0.868305, -0.868305, -0.857136, 0.857136, -0.846195, \
0.846195, 0.835525, -0.835525, -0.825179, 0.825179, -0.815217, \
0.815217, 0.805711, -0.805711, -0.796745, 0.796745, -0.788415, \
0.788415, -0.780827, 0.780827, -0.7741, 0.7741, -0.768358, 0.768358, \
-0.763727, 0.763727, 0.760322, -0.760322, -0.758237, 0.758237, \
-0.558185, 0.558185, -0.558185, 0.558185, -0.460503, 0.460503, \
-0.458199, 0.458199, -0.454443, 0.454443, -0.449346, 0.449346, \
-0.443042, 0.443042, -0.435671, 0.435671, -0.427369, 0.427369, \
0.418261, -0.418261, -0.40846, 0.40846, -0.398063, 0.398063, \
-0.387154, 0.387154, -0.375805, 0.375805, -0.364074, 0.364074, \
-0.352014, 0.352014, 0.339668, -0.339668, 0.32707, -0.32707, \
-0.314254, 0.314254, -0.301244, 0.301244, -0.288065, 0.288065, \
-0.274734, 0.274734, -0.26127, 0.26127, 0.247687, -0.247687, \
0.233997, -0.233997, -0.220213, 0.220213, -0.206345, 0.206345, \
-0.192401, 0.192401, -0.178389, 0.178389, -0.164318, 0.164318, \
-0.150193, 0.150193, -0.136022, 0.136022, -0.121809, 0.121809, \
-0.107559, 0.107559, 0.0932789, -0.0932789, 0.0789718, -0.0789718, \
-0.0646427, 0.0646427, -0.0502958, 0.0502958, 0.0359351, -0.0359351, \
-0.0215646, 0.0215646, -0.00718727, 0.00718727}

```

$\endgroup$
6
  • $\begingroup$ ClusteringComponents[l, 3] or FindClusters[l,3] $\endgroup$ Commented Jul 28, 2023 at 19:39
  • $\begingroup$ @DavidG.Stork Thanks for the suggestion. I tried this on my real data but it requires my input of the number of "components" to get the correct result, and they split the positive and negative segments but they are not gapped. Is there a way to avoid this? or better, a way to get it without specifying the number ofcomponents? $\endgroup$
    – wooohooo
    Commented Jul 28, 2023 at 20:12
  • 1
    $\begingroup$ Can you share your real data? The structure in the data might suggest a more effective approach. Otherwise look at SplitBy with an appropriate Difference function. $\endgroup$
    – MarcoB
    Commented Jul 28, 2023 at 20:45
  • 3
    $\begingroup$ @MarcoB I think you mean Split rather than SplitBy : SplitBy doesn't permit to compare two successive elements. $\endgroup$
    – andre314
    Commented Jul 28, 2023 at 21:07
  • 1
    $\begingroup$ For what it's worth, ClusteringComponents[l] finds 3 clusters. $\endgroup$
    – lericr
    Commented Jul 28, 2023 at 21:33

1 Answer 1

7
$\begingroup$
splitList=Split[l, #2-#1<=1&];(* 1 arbitrary chosen *)

splitList[[1]]==Range[-20,-15,0.05]
splitList[[2]]==Range[-10,10,0.01]
splitList[[3]]==Range[20,23,0.2]


(* True
   True
   True *) 

See the example "Split at jumps" in Split-> Generalizations and Extensions for a better explanation.

$\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.