find where a sequence of number breaks
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
pavlos
am 13 Dez. 2013
Kommentiert: pavlos
am 14 Dez. 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 Kommentare
Akzeptierte Antwort
Image Analyst
am 13 Dez. 2013
Bearbeitet: Image Analyst
am 13 Dez. 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 Kommentare
Weitere Antworten (1)
Azzi Abdelmalek
am 13 Dez. 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) ]
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!