Count total number of recurring sequences

1 view (last 30 days)
I would like to count how many times a consecutive sequence occurs in a list.
Let's say I have [0;1;1;1;0;0;0;0;1;0;1;1;1;1]
The output would be
1 3
2 0
3 1
4 2

Accepted Answer

Rik
Rik on 17 May 2021
If you get RunLength from the FileExchange this is fairly easy:
data=[0;1;1;1;0;0;0;0;1;0;1;1;1;1];
[~,N] = RunLength_M(data);
counts=histcounts(N,0.5:(max(N)+0.5))
counts = 1×4
3 0 1 2
output=[ ( 1:max(N) ).' counts.'];
disp(output)
1 3 2 0 3 1 4 2

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!