While end loop to find pattern in array

  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    Array Loop
AI Thread Summary
The discussion focuses on writing a while loop in MATLAB to count non-overlapping occurrences of a specified pattern in an array of 0's and 1's. The initial attempts included a for loop that allowed overlaps, but the challenge was to modify it for non-overlapping counts while incorporating a while loop. Key issues included correctly adjusting the index variable 'j' to ensure it skips ahead by the length of the pattern when a match is found, and resolving errors related to subscript indices. The final solution involved using the correct index adjustments and ensuring the while loop condition accurately reflects the bounds of the array. The conversation emphasizes the importance of understanding algorithm logic over trial and error.
gfd43tg
Gold Member
Messages
947
Reaction score
48

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.
 
Physics news on Phys.org
Wouldn't you write

Code:
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)
 
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 :)
 
Jedishrfu,

your code gives this error

Subscript indices must either be real
positive integers or logicals.
 
Maylis said:
Jedishrfu,

your code gives this error

Subscript indices must either be real
positive integers or logicals.

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.
 
Here is where I am now

Code:
numOccursSep=0;
j=1;
while j + length(pattern) - 1 < length(V)
    if isequal(V(j:j+length(pattern)-1),pattern)
        numOccursSep = numOccursSep+1;
        j=j+length(pattern)-1;
    else 
        j=j+1;
    end
end

I get this with the autograder
Code:
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:
Fixed it by changing j=j+length(pattern)-1; to j=j+length(pattern)
 
Maylis said:
Fixed it by changing j=j+length(pattern)-1; to j=j+length(pattern)

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