Count the number of times a particular sequence occurs in the vector.

3 views (last 30 days)
How to find the number of times and the index a sequence (here 01) occurs in the vector, using for loop?
The vector is randomly generated
A = round(rand(1000,1));

Answers (1)

Bruno Luong
Bruno Luong on 15 Nov 2022
Edited: Bruno Luong on 15 Nov 2022
Not for-loop but it does the job
A=randi(0:1,1,10)
A = 1×10
1 0 1 0 1 0 1 0 0 0
spattern = [0 1];
startidx = strfind(A,spattern)
startidx = 1×3
2 4 6

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!