Effacer les filtres
Effacer les filtres

find where a sequence of number breaks

6 vues (au cours des 30 derniers jours)
pavlos
pavlos le 13 Déc 2013
Commenté : pavlos le 14 Déc 2013
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 commentaires
dpb
dpb le 13 Déc 2013
Do you know the pattern a priori or do you have to discover it, too?
pavlos
pavlos le 14 Déc 2013
I know it a priori.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 13 Déc 2013
Modifié(e) : Image Analyst le 13 Déc 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

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 13 Déc 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) ]
  1 commentaire
pavlos
pavlos le 14 Déc 2013
Thank you very much.
Both methods work fine.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by