I have a list generated by:
list = RandomInteger[{1}, 100]
it contains only 0's and 1's
{0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, \
0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, \
1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, \
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, \
0, 1, 0, 0, 0, 0, 1, 1}
I'm trying to create a histogram in the following way: I want to look for two or more consecutive 1's e.g. 1,1 or 1,1,1 and so on. In other words I want to count all sequences of two or more consecutive 1's.
I also want to extract the positions of the found sequences in the list.
I tried to approach it with PatternSequence based on the help entry e.g.:
list //. {x___, PatternSequence[1, 1], y___} -> {x, "mypatterns", y}
which seems ok for showing me where the patterns in the list are, but it does not count them or give me the positions :(
Looking for some inspiration :)
Split
is you friend here :-) $\endgroup$Histogram[Length /@ Select[Split[list], First[#] == 1 &]]
. Positions:pos = Accumulate[Length /@ Split[list]]
. Positions of the ones having 1s:Pick[pos, First/@Split[list], 1]
. $\endgroup$