Take the 2-minute tour ×

I have multiple sets with a variable number of sequences. Each sequence is made of 64 numbers that are either 0 or 1 like so:

Set A

sequence 1: 0,0,0,0,0,0,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0

sequence 2: 0,0,0,0,1,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0

sequence 3: 0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0


Set B

sequence1: 0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1

sequence2: 0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0


I would like to find a mathematical function that describes all possible sequences in the set, maybe even predict more and that does not contain the sequences in the other sets.

I need this because I am trying to recognize different gestures in a mobile app based on the cells in a grid that have been touched (1 touch/ 0 no touch). The sets represent each gesture and the sequences a limited sample of variations in each gesture.

Ideally the function describing the sequences in a set would allow me to test user touches against it to determine which set/gesture is part of.

I searched for a solution, either using Excel or Mathematica, but being very ignorant about both and mathematics in general I am looking for the direction of an expert. Suggestions for basic documentation on the subject is also welcome.

share|improve this question
2^64 possible sequences? That's a lot to enumerate - how much time do you have? I'm not sure how you're going to "predict more" sequences than you've already got in "all possible sequences"... It might be useful if you could fill in a little more background - how did you arrive at these requirements ? –  Tim Williams Mar 23 '12 at 19:10
Instead of looking at all possible sequences I was looking for a function to define the ones that are already in the set and possibly to use as a ″rule″ to find more with the same pattern. –  Hermione333 Mar 23 '12 at 19:26
@Aglaia The question is too vague -- you have to specify what kind of patterns you have in mind. –  quant_dev Mar 23 '12 at 20:39
the problem is exactly that I do not know the pattern. I want to find out if there is one or at least find a function to describe all the sequences in the set so that in my app when a sequence is given I can test it against all the functions for all the sets. –  Hermione333 Mar 23 '12 at 21:45
i have absolutelly no idea what is the question here –  Aprillion Mar 23 '12 at 21:59

2 Answers 2

up vote 3 down vote accepted

It looks as if you are trying to treat what is essentially 2D data in 1D. For example, let s1 represent the first sequence in set A in your question. Then the command

ArrayPlot[Partition[s1, 8]]

produces this picture:

enter image description here

The other sequences in the same set produce similar plots. One of the sequences from the second set produces, in response to the same operations, the picture:

enter image description here

I don't know what sort of mathematical function you would like to define to describe these pictures, but I'm not sure that you need to if your objective is to recognise user gestures.

You could do something much simpler, such as calculate the 'average' picture for each of your gestures. One way to do this would be to calculate the average value for each of the 64 pixels in each of the pictures. Perhaps there are 6 sequences in your set A describing gesture A. Sum the sequences element-by-element. You will now have a sequence with values ranging from 0 to 6. Divide each element by 6. Now each element represents a sort of probability that a new gesture, one you are trying to recognise, will touch that pixel.

Repeat this for all the sets of sequences representing your set of gestures.

To recognise a user gesture, simply compute the difference between the sequence representing the gesture and each of the sequences representing the 'average' gestures. The smallest (absolute) difference will direct you to the gesture the user made.

I don't expect that this will be entirely foolproof, it may well result in some user gestures being ambiguous or not recognisable, and you may want to try something more sophisticated. But I think this approach is simple and probably adequate to get you started.

share|improve this answer
thank you. Your approach is definitely more in my comfort zone. I thought of something similar when I started but then I convinced myself there was a more precise method and that I could find a pattern that would separate the gestures beyond doubt. –  Hermione333 Mar 23 '12 at 23:18

In Mathematica the following expression will enumerate all the possible combinations of {0,1} of length 64.

Tuples[{1, 0}, {64}]

But there are 2^62 or 18446744073709551616 of them, so I'm not sure what use that will be to you.

Maybe you just wanted the unique sequences contained in each set, in that case all you need is the Mathematica Union[] function applied to the set. If you have a the sets grouped together in a list in Mathematica, say mySets, then you can apply the Union operator to every set in the list my using the map operator.


If you want to do some type of prediction a little more information might be useful.

Thanks you for the clarifications.

Machine Learning

The task you want to solve falls under the disciplines known by a variety of names, but probably most commonly as Machine Learning or Pattern Recognition and if you know which examples represent the same gestures, your case would be known as supervised learning.

Question: In your case do you know which gesture each example represents ?

You have a series of examples for which you know a label ( the form of gesture it is ) from which you want to train a model and use that model to label an unseen example to one of a finite set of classes. In your case, one of a number of gestures. This is typically known as classification.

Learning Resources

There is a very extensive background of research on this topic, but a popular introduction to the subject is machine learning by Christopher Bishop. Stanford have a series of machine learning video lectures Standford ML available on the web.


You might want to consider how you will determine the accuracy of your system at predicting the type of gesture for an unseen example. Typically you train the model using some of your examples and then test its performance using examples the model has not seen. The two of the most common methods used to do this are 10 fold Cross Validation or repeated 50/50 holdout. Having a measure of accuracy enables you to compare one method against another to see which is superior.

Have you thought about what level of accuracy you require in your task, is 70% accuracy enough, 85%, 99% or better?

Machine learning methods are typically quite sensitive to the specific type of data you have and the amount of examples you have to train the system with, the more examples, generally the better the performance.

You could try the method suggested above and compare it against a variety of well proven methods, amongst which would be Random Forests, support vector machines and Neural Networks. All of which and many more are available to download in a variety of free toolboxes.


Mathematica is a wonderful system, is infinitely flexible and my favourite environment, but out of the box it doesn't have a great deal of support for machine learning.

I suspect you will make a great deal of progress more quickly by using a custom toolbox designed for machine learning. Two of the most popular free toolboxes are WEKA and R both support more than 50 different methods for solving your task along with methods for measuring the accuracy of the solutions.

With just a little data reformatting, you can convert your gestures to a simple file format called ARFF, load them into WEKA or R and experiment with dozens of different algorithms to see how each performs on your data. The explorer tool in WEKA is definitely the easiest to use, requiring little more than a few mouse clicks and typing some parameters to get started.

Once you have an idea of how well the established methods perform on your data you have a good starting point to compare a customised approach against should they fail to meet your criteria.

Handwritten Digit Recognition

Your problem is similar to a very well researched machine learning problem known as hand written digit recognition. The methods that work well on this public data set of handwritten digits are likely to work well on your gestures.

share|improve this answer
I do not need all the possible combinations but just the ones in a set. Is it possible to find a function that describes all the sequences in the set and not just one? Thank you for your help –  Hermione333 Mar 23 '12 at 19:24
@Aglaia it is possible. The command Union/@mySets would give you the answer you require , as detailed in my answer. Happy to explain more if the answer is not clear. –  image_doctor Mar 23 '12 at 19:36
Union is giving me the list of all the sequences but not a mathematical function to describe them; I was thinking of something like FindSequenceFunction but for the entire set of sequences. –  Hermione333 Mar 23 '12 at 19:49
Sorry for my misunderstanding. You may need to be a little more precise about what you mean "describe". The list of the sequences is itself a "description" of the sequences. You could wrap the list up inside a function that returned a sequences as a function of an integer index. Such as MakeSequenceFunction[sequences_] := Function[n, sequences[[n]]]. You could look at FindLinearRecurrence or FindGeneratingFunction. Maybe what might help is the motivation as to why you need a function to describe a sequence you already have defined ? –  image_doctor Mar 23 '12 at 20:33
I am sorry for the vagueness, but it is the first day I open Mathematica :-). I will look into all of the above suggestions immediately. The fact is I have an incomplete sample of user touches that I want to recognize and each sequence in a set represents an instance of the user touches in a grid of 64 cells (1 for cell touched/ 0 for cell not touched); each set represents a particular gesture in the grid with all the variations that I found up until now. That is why I would like to find a general function to describe each set so as to verify in my app the gesture of the user. –  Hermione333 Mar 23 '12 at 21:02

Your Answer


