find where a sequence of number breaks
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2013-12-13
编辑:Image Analyst
2013-12-13
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 个评论
更多回答(1 个)
Azzi Abdelmalek
2013-12-13
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) ]
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!