This site uses strictly necessary cookies. More Information

X- Home /

# Finding a pattern in an array

Say I had the array:

`int [] measurment = {1, 1, 9, 0, 7, 5, 1, 9, 8, 3, 3};`

and the array:

`int [] pattern = {3, 3};`

I'm trying to get have my code find the pattern (3, 3) in the measurement array and then print out the location of the beginning of the pattern in the console. I'm also trying to get it to find the closest thing to the pattern if the pattern doesn't exist. This is what I have so far:

```
void Start ()
{
int [] measurment = {1, 1, 9, 0, 7, 5, 1, 9, 8, 3, 3};
int [] pattern = {3, 3};
int [] patMatch;
for (int i = 0; i < pattern.length; i += 1){
for (int p = 0; p < measurment.length; p += 1){
if(pattern[i] == measurment[p]){
for (int x = 0; x < pattern.length; x += 1){
if(pattern[x] == measurment[x + 1]){
//not sure what to do next
}
}
}
}
}
}
}
```

Pattern recognition can be difficult. @robertbu's solution is valid for your particular use case. But if you want a more general pattern recognition algorithm you might want to consider using a neural net.

**Answer** by robertbu
·
Oct 15, 2014 at 01:40 AM

```
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void Start ()
{
int [] measurement = {1, 1, 9, 0, 7, 5, 1, 9, 8, 3, 3, 4};
int [] pattern = {9, 8, 3};
float percent;
int ii = BestMatch(measurement, pattern, out percent);
percent *= 100.0f;
Debug.Log ("Best match is at index " + ii + " at " + percent + " percent.");
}
int BestMatch(int[] source, int[] pattern, out float percent) {
int iBestMatch = 0;
int iMatched = 0;
for (int i = 0; i < source.Length - pattern.Length + 1; i++) {
int count = 0;
for (int j = 0; j < Mathf.Min (pattern.Length, source.Length); j++) {
if (source[i + j] == pattern[j]) {
count++;
}
}
if (count == pattern.Length) {
percent = 1.0f;
return i;
}
if (count > iMatched) {
iMatched = count;
iBestMatch = i;
}
}
percent = (float)iMatched / pattern.Length;
return iBestMatch;
}
}
```

You could increase the precision of this algorithm by calculating an Rsquared value and minimising it.

If the measurement array had the pattern twice or more how would I add the functionality to find all the matches? And I should have been more clear on the closest pattern. Say:

```
int [] measurement = {1, 1, 9, 0, 7, 5, 1, 8, 9, 4, 3, 4};
int [] pattern = {9, 8, 3};
```

the pattern doesn't exist in the measurement array, but {8, 9, 3} is only off the pattern by 3 (if you add together and subtract the difference). How would I find that?

For multiple matches, you could either rewrite the code so that it returns a generic list of the index of any 100 percent matches, or add the starting index in the source and call the function multiple times until it returns a pattern that is not a 100 percent match.

As for your distance calculation, the scanning code would be the same. 'count' would become the sum of the absolute value of the differences. Line 27, would check if 'count' was 0 (i.e. no differences), and count would be initialized to some high integer value.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.