title: c++ confusion matrix error
body: <p>I'm trying to build a confusion matrix from a set of SVM prediction based on the OpenCV SVM train and predict function as so:</p>
  
  <pre><code>void testClassifier(CvSVM &amp; SVM, 
      std::vector&lt;std::vector&lt;float&gt;&gt; &amp; test_samples,
      std::vector&lt;float&gt; &amp; test_labels,
      const CvSVMParams &amp; params,
      double &amp; tempAcc)
  {
      cv::Mat matSamp;
      int response;
      int cnt = 0;
      //std::vector&lt;int&gt; labels = { 0, 1, 2, 3, 4, 5, 6 };
      std::vector&lt;int&gt; exCount = { 0, 0, 0, 0, 0, 0, 0 };
      std::vector&lt;std::vector&lt;double&gt;&gt; confMat = { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } };
  
  
      //test classifier
      for (unsigned int t = 0; t &lt; test_samples.size(); t++)
      {
          matSamp = cv::Mat(test_samples[t].size(), 1, CV_32FC1, test_samples[t].data());
          response = SVM.predict(matSamp);
  
          if (response == test_labels[t])
          {
              cnt++;
          }
  
          exCount[test_labels[t]] += 1;
          //cout &lt;&lt; test_labels[t] &lt;&lt; " " &lt;&lt; exCount[test_labels[t]] &lt;&lt; endl;
          confMat[test_labels[t]][response] += 1.0;
          //cout &lt;&lt; confMat[test_labels[t]][response] &lt;&lt; endl;
      }
  
      cout &lt;&lt; "Parameters used: " &lt;&lt; endl;
      tempAcc = (double)cnt * 100 / test_samples.size();
      cout &lt;&lt; "  C:               " &lt;&lt; params.C &lt;&lt; endl;
      cout &lt;&lt; "  Gamma:           " &lt;&lt; params.gamma &lt;&lt; endl;
      cout &lt;&lt; "you hit " &lt;&lt; tempAcc &lt;&lt; "% accuracy" &lt;&lt; endl &lt;&lt; endl;
      cout &lt;&lt; confMat[0][0] / exCount[0] * 100 &lt;&lt; " " &lt;&lt; confMat[0][1] / exCount[1] * 100 &lt;&lt; " " &lt;&lt; confMat[0][2] / exCount[2] * 100 &lt;&lt; " " &lt;&lt; confMat[0][3] / exCount[3] * 100 &lt;&lt; " " &lt;&lt; confMat[0][4] / exCount[4] * 100 &lt;&lt; " " &lt;&lt; confMat[0][5] / exCount[5] * 100 &lt;&lt; " " &lt;&lt; confMat[0][6] / exCount[6] * 100 &lt;&lt; endl;
      cout &lt;&lt; confMat[1][0] / exCount[0] * 100 &lt;&lt; " " &lt;&lt; confMat[1][1] / exCount[1] * 100 &lt;&lt; " " &lt;&lt; confMat[1][2] / exCount[2] * 100 &lt;&lt; " " &lt;&lt; confMat[1][3] / exCount[3] * 100 &lt;&lt; " " &lt;&lt; confMat[1][4] / exCount[4] * 100 &lt;&lt; " " &lt;&lt; confMat[1][5] / exCount[5] * 100 &lt;&lt; " " &lt;&lt; confMat[1][6] / exCount[6] * 100 &lt;&lt; endl;
      cout &lt;&lt; confMat[2][0] / exCount[0] * 100 &lt;&lt; " " &lt;&lt; confMat[2][1] / exCount[1] * 100 &lt;&lt; " " &lt;&lt; confMat[2][2] / exCount[2] * 100 &lt;&lt; " " &lt;&lt; confMat[2][3] / exCount[3] * 100 &lt;&lt; " " &lt;&lt; confMat[2][4] / exCount[4] * 100 &lt;&lt; " " &lt;&lt; confMat[2][5] / exCount[5] * 100 &lt;&lt; " " &lt;&lt; confMat[2][6] / exCount[6] * 100 &lt;&lt; endl;
      cout &lt;&lt; confMat[3][0] / exCount[0] * 100 &lt;&lt; " " &lt;&lt; confMat[3][1] / exCount[1] * 100 &lt;&lt; " " &lt;&lt; confMat[3][2] / exCount[2] * 100 &lt;&lt; " " &lt;&lt; confMat[3][3] / exCount[3] * 100 &lt;&lt; " " &lt;&lt; confMat[3][4] / exCount[4] * 100 &lt;&lt; " " &lt;&lt; confMat[3][5] / exCount[5] * 100 &lt;&lt; " " &lt;&lt; confMat[3][6] / exCount[6] * 100 &lt;&lt; endl;
      cout &lt;&lt; confMat[4][0] / exCount[0] * 100 &lt;&lt; " " &lt;&lt; confMat[4][1] / exCount[1] * 100 &lt;&lt; " " &lt;&lt; confMat[4][2] / exCount[2] * 100 &lt;&lt; " " &lt;&lt; confMat[4][3] / exCount[3] * 100 &lt;&lt; " " &lt;&lt; confMat[4][4] / exCount[4] * 100 &lt;&lt; " " &lt;&lt; confMat[4][5] / exCount[5] * 100 &lt;&lt; " " &lt;&lt; confMat[4][6] / exCount[6] * 100 &lt;&lt; endl;
      cout &lt;&lt; confMat[5][0] / exCount[0] * 100 &lt;&lt; " " &lt;&lt; confMat[5][1] / exCount[1] * 100 &lt;&lt; " " &lt;&lt; confMat[5][2] / exCount[2] * 100 &lt;&lt; " " &lt;&lt; confMat[5][3] / exCount[3] * 100 &lt;&lt; " " &lt;&lt; confMat[5][4] / exCount[4] * 100 &lt;&lt; " " &lt;&lt; confMat[5][5] / exCount[5] * 100 &lt;&lt; " " &lt;&lt; confMat[5][6] / exCount[6] * 100 &lt;&lt; endl;
      cout &lt;&lt; confMat[6][0] / exCount[0] * 100 &lt;&lt; " " &lt;&lt; confMat[6][1] / exCount[1] * 100 &lt;&lt; " " &lt;&lt; confMat[6][2] / exCount[2] * 100 &lt;&lt; " " &lt;&lt; confMat[6][3] / exCount[3] * 100 &lt;&lt; " " &lt;&lt; confMat[6][4] / exCount[4] * 100 &lt;&lt; " " &lt;&lt; confMat[6][5] / exCount[5] * 100 &lt;&lt; " " &lt;&lt; confMat[6][6] / exCount[6] * 100 &lt;&lt; endl;
      cout &lt;&lt; "enter for next matrix" &lt;&lt; endl;/**/
      cin.get();
  }
  </code></pre>
  
  <p>but when the matrix entries are outputted some rows sum to greater than 100% and some less. I'm sure its something simple but I've been staring at it for ages with no clue. any ideas?</p>
  
  <hr>
  
  <h1>UPDATE</h1>
  
  <p>Im not sure why but the multiple <code>cout</code> I was using affected the return value.</p>
  
  <p>The problem was solved by including the debugging <code>cout</code> in a for loop like this:</p>
  
  <pre><code>for (unsigned int m = 0; m &lt; 7; m++)
  {
      for (unsigned int n = 0; n &lt; 7; n++)
      {
          confMat[m][n] = confMat[m][n] * 100 / exCount[m];
          cout &lt;&lt; confMat[m][n] &lt;&lt; " ";
      }
      cout &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Anyone does know why?</p>