- #1
gfd43tg
Gold Member
- 950
- 50
Homework Statement
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 first 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.
Homework Equations
The Attempt at a Solution
Here is my loop for when overlap is allowed to occur
Code:
numOccurs = 0;
for j = length(pattern):length(V)
if V(j-length(pattern)+1:j) == pattern;
numOccurs = numOccurs + 1;
end
end
Now I am totally stuck for how I will be able to find how many times the pattern appears without overlap permitted. On top of that, now I have to incorporate a while loop. This is what I have so far.
Code:
j=length(pattern);
numOccursSep=0;
while j >= length(V)-length(pattern)
if V(j:j-length(pattern)+1) == pattern;
numOccursSep = numOccursSep + 1;
end
end
This doesn't give the right answer. I just don't know where to go with this. I don't even know what conditional statement to use for my while statement, and I am just fiddling with j's, length(..), and +/- 1 inside V(...) until something would happen. I don't see where to go with this at all.