# Homework Help: While end loop to find pattern in array

1. Jun 23, 2014

### Maylis

1. The problem statement, all variables and given/known data
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.

2. Relevant equations

3. The attempt at a solution
Here is my loop for when overlap is allowed to occur
Code (Text):
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 (Text):
j=length(pattern);
while j >= length(V)-length(pattern)
if V(j:j-length(pattern)+1) == pattern;
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.

2. Jun 23, 2014

### Staff: Mentor

Wouldn't you write

Code (Text):

plen=length(pattern);
pcount=0;

vlen=length(v);
j=0;

while j<vlen
if v(j:j+plen)==pattern
pcount=pcount+1;
j=j+plen;
else
j=j+1;
end
end

The reason for the while loop is that you can adjust the j index to whatever value is needed so when you don't find the pattern you advance one step and when you do you advance j+plen steps (for the non-overlapping part)

3. Jun 23, 2014

### cpscdave

There are a couple problems with what you have done (I'm quite rusty on Matlab code so bear with me)
First off check your while statement. Something seems backwards to me :)
Additionally think about how j needs to change through out the loop.
How would it change each iteration of the loop?
Would it change (and if so how?) when it matches a pattern? What about if it doesn't?

I know it's tempting to fiddle with values, and goodness knows I've done it many many MANY times, but it almost never ends up giving me a solution. Better to step back and think about what you're doing, if its a problem like this run the algorthim on paper and think about how you would solve the problem :)

4. Jun 23, 2014

### Maylis

Jedishrfu,

Subscript indices must either be real
positive integers or logicals.

5. Jun 23, 2014

### Staff: Mentor

I'm sorry I tend to write in pseudo code to give you an idea of the solution. Remember PF can't just give you the solution.

So look at it and see how it walks thru the V array looking for the pattern.

6. Jun 23, 2014

### Maylis

Here is where I am now

Code (Text):
j=1;
while j + length(pattern) - 1 < length(V)
if isequal(V(j:j+length(pattern)-1),pattern)
j=j+length(pattern)-1;
else
j=j+1;
end
end
I get this with the autograder
Code (Text):
Problem 4e: 2/3
*the value of numOccursSep is incorrect for the variables: V = [1 1 1 1 1 1 1 1 1]; pattern = [1 1];
Your value deviates from the expected value by more than a tolerance

Last edited: Jun 23, 2014
7. Jun 23, 2014

### Maylis

Fixed it by changing j=j+length(pattern)-1; to j=j+length(pattern)

8. Jun 23, 2014

### Staff: Mentor

Do you fix this in both places or only one place? You have the expression in your while condition and in j=j+... statement.

9. Jun 23, 2014

### Maylis

only the while condition