title: Classification with Matlab. Recognize classes in the test set
body: <p>I have a situation that seems trivial but I can't figure it out. I have a dataset in Matlab that has categorical values. For example:</p>
  
  <pre><code>Outlook,Temperature,Humidity,Windy,Play
  sunny,hot,high,false,no
  sunny,hot,high,true,no
  overcast,hot,high,false,yes
  rainy,mild,high,false,yes
  rainy,cool,normal,false,yes
  rainy,cool,normal,true,no
  overcast,cool,normal,true,yes
  </code></pre>
  
  <p>In order to use ClassificationTree (or other algorithm in machine learning), I need to convert the dataset to a matrix. I use (assuming that <em>data</em> contains the dataset):</p>
  
  <pre><code>double(data)
  </code></pre>
  
  <p>which assigns numbers to the values. Let's say rainy=1, overcast=2, sunny=3 and so on. I train the ClassificationTree and it works fine. But here is my problem. If I want to predict on test data:</p>
  
  <pre><code>overcast,cool,normal,false,yes
  </code></pre>
  
  <p>I need to know the numbers that double() assigned to each of the categories. i.e. overcast=2. Using double() on the test set does not work because there is no guarantee that the numbers assigned by double() are the same to those assigned to the <em>training</em> dataset.</p>
  
  <p>I have found a really twisted way of doing it. For example, for Outlook:</p>
  
  <pre><code>d = zeros(size(test));
  ls = getlevels(training.Outlook);
  n = size(ls,2);
  for i = 1:n
      d(test.Outlook == ls(i),1) = i;
  end
  </code></pre>
  
  <p>Which assigns the numbers correctly because I check against each of the values for Outlook that I found in the <em>training</em> dataset. This is far from elegant, and there has to be something better. </p>
  
  <p>Any help is appreciated.</p>