1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

While end loop to find pattern in array

  1. Jun 23, 2014 #1

    Maylis

    User Avatar
    Gold Member

    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);
    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.
     
  2. jcsd
  3. Jun 23, 2014 #2

    jedishrfu

    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)
     
  4. Jun 23, 2014 #3
    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 :)
     
  5. Jun 23, 2014 #4

    Maylis

    User Avatar
    Gold Member

    Jedishrfu,

    your code gives this error

    Subscript indices must either be real
    positive integers or logicals.
     
  6. Jun 23, 2014 #5

    jedishrfu

    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.
     
  7. Jun 23, 2014 #6

    Maylis

    User Avatar
    Gold Member

    Here is where I am now

    Code (Text):
    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 (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
  8. Jun 23, 2014 #7

    Maylis

    User Avatar
    Gold Member

    Fixed it by changing j=j+length(pattern)-1; to j=j+length(pattern)
     
  9. Jun 23, 2014 #8

    jedishrfu

    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.
     
  10. Jun 23, 2014 #9

    Maylis

    User Avatar
    Gold Member

    only the while condition
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: While end loop to find pattern in array
  1. While loop (Replies: 1)

  2. Matlab while loop help (Replies: 15)

  3. C++- while-loop (Replies: 7)

Loading...