Comments and answers for "Finding a pattern in an array"
http://answers.unity.com/questions/809441/finding-a-pattern-in-an-array.html
The latest comments and answers for the question "Finding a pattern in an array"Comment by robertbu on robertbu's answer
http://answers.unity.com/comments/810139/view.html
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.Wed, 15 Oct 2014 20:36:10 GMTrobertbuComment by zefrof on zefrof's answer
http://answers.unity.com/comments/809914/view.html
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?Wed, 15 Oct 2014 15:41:56 GMTzefrofComment by Kiwasi on Kiwasi's answer
http://answers.unity.com/comments/809502/view.html
You could increase the precision of this algorithm by calculating an Rsquared value and minimising it.Wed, 15 Oct 2014 03:39:12 GMTKiwasiComment by Kiwasi
http://answers.unity.com/comments/809496/view.html
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.Wed, 15 Oct 2014 03:19:49 GMTKiwasiAnswer by robertbu
http://answers.unity.com/answers/809455/view.html
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;
}
}Wed, 15 Oct 2014 01:40:53 GMTrobertbu