using while-end block to find pattern in an array

조회 수: 1 (최근 30일)
Rick
Rick 2014년 6월 22일
편집: Andrei Bobrov 2014년 6월 23일
Write code using a while loop that will assign to the variable numOccursSep the number of times a certain pattern of 0's and 1's occurs separately in V. The variable pattern gives the certain pattern to look for. For this problem overlap is not allowed, i.e. if pattern = [0, 1, 0] then the last 0 of an occurrence of [0, 1, 0] cannot be the rst 0 of the next occurrence. For example, if V = [0, 1, 0, 1, 0] then only one instance of [0, 1, 0] should be counted and numOccursSep should equal 1.
V = [0, 1, 0, 1, 0];
pattern = [0, 1, 0];
count = 0;
while strfind(V,pattern) == 1
count = count+1;
end
numOccursSep = count;
Normally I would use strfind(V,pattern) and then numel(strfind(V,pattern)) but I do not know how to account for the fact that overlap is not allowed. How can I do this?
  댓글 수: 9
Rick
Rick 2014년 6월 23일
편집: Rick 2014년 6월 23일
Is this what you meant by '' loop over the position of a starting substring to search and only search for the length of the pattern in the string. If found, increment the indices by length(pattern), if not then increment by one.'' The part j < length(V) doesnt seem to make sense to me. That is always true, but the while loop is supposed to run until a certain condition is met, of which it always is.
j=length(pattern);
numOccursSep=0;
while j < length(V)
if strfind(V(1:j),pattern)
j + length(pattern);
else
j + 1;
end
end
dpb
dpb 2014년 6월 23일
편집: dpb 2014년 6월 23일
Closer, and "sorta'" :) Note in my implementation there are two position indicators, not just one and the loop control is over the second. These two are the start:end locations for the next part of the string to search; hence the loop runs until the new end position runs past the length(searchstring). You've recast it to only one and keep searching from the beginning instead of moving the starting point along...the rest of my implementation is
count = count+1; % found a match, increment by L
i1=i2+1;i2=i2+L;
else % no match, no count, increment by 1
i1=i1+1;i2=i2+1;
end
end

댓글을 달려면 로그인하십시오.

답변 (2개)

dpb
dpb 2014년 6월 23일
Putting comments together since did put in the effort to try...
L=length(pattern); % keep the constant
i1=1;i2=L; % start w/ beginning of string length of pattern
count=0;
while i2<length(V)
if strfind(V(i1:i2),pattern) % found, so
count = count+1; % increment count
i1=i2+1;i2=i2+L; % next start is previous end+1, length L
else % not found, so
i1=i1+1;i2=i2+1; % look in next position
end
end
I'll still note one can use the raw output of strfind on the whole vector and post-process the found locations with diff to discover which are not disparate subsections to remove the overlapped cases.

Andrei Bobrov
Andrei Bobrov 2014년 6월 23일
편집: Andrei Bobrov 2014년 6월 23일
m = numel(pattern);
ii = strfind(V(:)',pattern(:)');
k = ii(1);
f = ii(2:end);
count = 1;
while numel(f) >= 1
count = count + 1;
f = f((k + m - 1) < f);
k = f(1);
f = f(2:end);
end
other variant witout while loop
m = numel(pattern);
x = zeros(1,numel(V));
x(bsxfun(@plus,strfind(V(:)',pattern(:)'),(0:m-1)')) = 1;
t = sum(floor((strfind([x 0],[1 0])...
- strfind([0 x],[0 1]) + 1)/m));

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by