Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zefrof · Oct 15, 2014 at 12:52 AM · c#arrayfor-loop

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
                             }
                         }
                     }
                 }
             }
         }
    }



Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Oct 15, 2014 at 03:19 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
     }
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Oct 15, 2014 at 03:39 AM 0
Share

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

avatar image zefrof · Oct 15, 2014 at 03:41 PM 0
Share

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?

avatar image robertbu · Oct 15, 2014 at 08:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to deactivate all GameObject in a array, except last one 4 Answers

C# For loop in button to set gameObjects in array to active 1 Answer

Error CS0029 Help? (Screenshot of Exact Error) 1 Answer

Array Overflow Problem 1 Answer

How to save a 2d-array in C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges