find where a sequence of number breaks
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
pavlos
el 13 de Dic. de 2013
Comentada: pavlos
el 14 de Dic. de 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 comentarios
Respuesta aceptada
Image Analyst
el 13 de Dic. de 2013
Editada: Image Analyst
el 13 de Dic. de 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
0 comentarios
Más respuestas (1)
Azzi Abdelmalek
el 13 de Dic. de 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) ]
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!