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:
SplitBy
performs 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.
Answer
SplitBy
is a wrong tool for the job here, because the condition you need couples adjacent elements in a way which can not be decoupled by applying some function to them. I would use
Split[mylist, Chop[#2 - #1 - 0.1] == 0 &]
Comments
Post a Comment