Suppose that I have the following list called mylist:
mylist = {80.2, 80.3, 80.4, 327.6, 327.7, 327.8};
I would like to split mylist so that "runs of consecutive elements" are placed in the same sublist. Here, I will define two "consecutive elements" as real numbers that differ by 0.1. The second element can be either 0.1 greater than or 0.1 less than the first element, although in reality it makes more sense if the second element can only be 0.1 greater than the first element. (In my actual application, mylist is a list of times [i.e., elapsed time], in seconds, for example. My system is sampled every 0.1 seconds. Time never moves backwards, at least in my world!)
So the result I would like is:
{{80.2, 80.3, 80.4}, {327.6, 327.7, 327.8}}
I am thinking that SplitBy would be appropriate for this application:
SplitBy[list, f]splits list into sublists consisting of runs of successive elements that give the same value when f is applied.
However, in looking in the documentation, it is not clear to me how "adjacent elements" are defined. In Mathematica 7, "More Information" says:
SplitByperforms comparisons only on adjacent pairs elements.
Thus, what f do I use? I have tried:
SplitBy[mylist, #[[2]] - #[[1]] == 0.1 &]
and
SplitBy[mylist, #2 - #1 == 0.1 &]
but neither work. Does SplitBy define separate "slots" for the first and second element of a pair? Thanks for your time.
SplitByis mentioned in the question, it can not be used for cases such as this, and this question (or rather answers to it) will emphasize that. – Leonid Shifrin Oct 02 '12 at 22:03SplitandSplitBy, and your answer helps me to learn this. Thanks. – Andrew Oct 02 '12 at 22:05SplitBycannot be used. – rm -rf Oct 02 '12 at 22:06