find where a sequence of number breaks

4 views (last 30 days)
Hello,
Please help me with the following.
Suppose with have the sequence pattern 2 3 4 and we want to find where it breaks when it is found in a row.
For example [1 1 1 2 3 4 2 3 4 2 3 4 9 9 2 3 4 2 3 4 0 5]
The sequence is repeated for 3 times and then it breaks and again it is repeated 2 more times.
How we can find the number of repeats until the next break occurs?
The outcome of method or function should be:
ans=[3 2], where 3 are the repetitions until the first break and 2 are the repetitions until the second break.
Thank you. Best,
Pavlos
  2 Comments
dpb
dpb on 13 Dec 2013
Do you know the pattern a priori or do you have to discover it, too?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 13 Dec 2013
Edited: Image Analyst on 13 Dec 2013
Do you have the Image Processing Toolbox? It's pretty easy if you do:
m = [1 1 1 2 3 4 2 3 4 2 3 4 9 9 2 3 4 2 3 4 0 5]
p = [2,3,4] % The pattern.
pl = length(p); % Length of the pattern
% Find starting points of where the pattern occurs.
matches = strfind(m, p)
% Mark which elements of the input matrix are in the pattern.
inPattern = false(1, length(m));
for k = 1 : length(matches)
inPattern(matches(k):matches(k)+pl-1) = true;
end
% Measure the lengths of the regions that are "in pattern"
measurements = regionprops(inPattern, 'Area');
% Divide by the length of the pattern to get the number of patterns.
numPatterns = [measurements.Area] / pl

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 13 Dec 2013
v=[2 3 4 2 3 4 1 2 2 1 2 3 4 2 3 4 2 3 4 9 9 2 3 4 2 3 4 0 5 2 3 4 2 3 4 0]
v1=num2str(v);
v1=strrep(v1,' ','');
ii=regexp(v1,'234','start');
jj=[ 3 diff(ii)];
jj(jj~=3)=0;
a=unique([strfind([jj 0],[3,0]) numel(jj)]);
out=[a(1) diff(a) ]

Community Treasure Hunt

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

Start Hunting!