I am trying to effectively find a number which tells me the number of sequences of ones in a vector of zeros and ones. For example, consider the vector:
{1,1,0,0,1,1,1,0,1}
My question is, how many {1,1}
does it contain, while I don't want to consider {1,1,1} like 2 times series of {1,1}. So the answer should be 1 not 3.
Of course I want to find out answer for sequences of {1,1,1} , {1,1,1,1}...
to the longest sequence, in some different series of zeros and ones.
Is there some nice built in function which can help me please ?
To Addendum:
Thanks again, yes algorithm using Tally works much better, actually a need it for this function which is calculating from vector input RQA analysis, If you have some suggestion how to speed up I would be glad. I want to use it on vector of length 10^4 and now it takes on my machine like 70 seconds.
ClearAll[Rp1];
Rp1[data_] :=
Module[{rr, N1, idx, R, SR, RR, DDE, DET, i, j, DE},
rr = N[Variance[data]];
N1 = Length[data];
R = ConstantArray[0, {N1, N1}];
idx = Nearest[data -> Automatic, data, {\[Infinity], Sqrt[rr]}][[
All, 2 ;;]];
MapIndexed[{x, y} \[Function] R[[y, x]] = 1, idx];
R;
SR = Total[Total[R]];
RR = SR/(N1*(N1 - 1));
oneCount[list_, len_] :=
Total@UnitStep[
ListCorrelate[ArrayPad[ConstantArray[1, len], 1, -1],
list, {2, -2}, 0] - len];
DE = Total[
Table[oneCount[Diagonal[R, i], 1], {i, 1,
Length[Diagonal[R, 1]]}]] ;
DET = (SR - 2*DE )/SR;
N[{RR, DET}]
]