While end loop to find pattern in array

In summary, the code assigns the value numOccursSep=1 to the variable when the pattern is [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1], and the value numOccursSep=0 when the pattern is anything else.
  • #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.
 
Physics news on Phys.org
  • #2
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)
 
  • #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 :)
 
  • #4
Jedishrfu,

your code gives this error

Subscript indices must either be real
positive integers or logicals.
 
  • #5
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.
 
  • #6
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:
  • #7
Fixed it by changing j=j+length(pattern)-1; to j=j+length(pattern)
 
  • #8
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.
 
  • #9
only the while condition
 

1. How do you use a while loop to find a pattern in an array?

A while loop can be used to iterate through an array and check for a specific pattern. The loop starts by setting a counter variable to 0, which will represent the index of the array. Then, a while loop is used to continue iterating through the array until the pattern is found or the end of the array is reached. Within the loop, the counter variable is incremented to move to the next index in the array. The pattern can be checked using conditional statements or other logic within the loop.

2. Can a while loop find multiple occurrences of a pattern in an array?

Yes, a while loop can be used to find multiple occurrences of a pattern in an array. As mentioned before, the loop will continue to iterate until the pattern is found or the end of the array is reached. If the pattern is found, the loop can be set to continue searching for the next occurrence by not breaking out of the loop. This can be done by using a boolean variable or other condition to control the loop.

3. What happens if the pattern is not found in the array using a while loop?

If the pattern is not found in the array using a while loop, the loop will continue to iterate until the end of the array is reached. At this point, the loop will end and the code after the loop will be executed. This can be used to handle cases where the pattern is not found, such as displaying a message to the user or performing other actions.

4. Is a while loop the most efficient way to find a pattern in an array?

The efficiency of using a while loop to find a pattern in an array depends on the size of the array and the complexity of the pattern being searched for. In some cases, there may be more efficient ways to search for a pattern, such as using built-in array methods like indexOf() or regular expressions. It is important to consider the specific requirements of the task at hand when choosing the most efficient method.

5. Can a while loop be used to find a pattern in a multidimensional array?

Yes, a while loop can be used to find a pattern in a multidimensional array. The same principles apply, except now the loop will need to iterate through both the rows and columns of the array. The counter variables for each dimension will need to be incremented separately and the pattern can be checked using nested loops or other logic. It is important to keep track of the indices in a multidimensional array to ensure the loop is searching through the correct elements.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
704
  • Engineering and Comp Sci Homework Help
Replies
1
Views
934
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top