title: Why am I getting "nan" values in C++?
body: <p>I am making a Correlogram for an image. For each pixel, a correlogram finds the pixels of same color within a certain range of distance, d. Correlogram is a 2D matrix i.e. <code>correlogram[color][distance]</code>. The calculation of a Correlogram is somewhat similar to that of a Histogram. </p>
  
  <p><strong>My Code:</strong>  I am posting some major part of the code in which all the calculations are going on. Rest of the code (which i didn't post) is used to fulfill other condtions and therefore is not necessary.</p>
  
  <p><strong>Problem:</strong> In my final <code>correlogram[][]</code> , some values are "nan". I have checked the code but i am not able to find where is the problem in my calculation/syntax.</p>
  
  <pre><code>int ColorBins = 180;
  int DistanceRange = 5;
  
  double calcCorrelogram(Mat hsvImage)
  {
      double correlogram[ColorBins][DistanceRange];
      int pixelNum[ColorBins]; //Used to count the number of pixels of same color
  
      Mat hsvPlanes[3];
      split(hsvImage, hsvPlanes);
  
      for(int pi=0; pi&lt;hsvImage.rows; pi++)
      {
          for(int pj=0; pj&lt;hsvImage.cols; pj++)
          {
              int pixelColor = (int)hsvPlanes[0].at&lt;uchar&gt;(pi,pj);
              pixelNum[pixelColor]++; 
  
              for(int d=1; d&lt;=DistanceRange; d++)
              {
                  int sameColorNum=0; //* number of pixels with same color in the d-distance boundary */
                  int totalBoundaryNum=0; //* total number of pixels in the d-distance boundary */
  
                  for(int i= pi-d, j= pj-d; j&lt;=pj+d; j++)
                  {
                      if(i&lt;0)
                          break;
                      if(j&lt;0 || j&gt;=hsvImage.cols)
                          continue;
  
                      int neighbourColor  = (int)hsvPlanes[0].at&lt;uchar&gt;(i,j);
                      if(pixelColor == neighbourColor)
                      {
                          sameColorNum++;
                      }
                      totalBoundaryNum++;
  
                      correlogram[pixelColor][d-1] = correlogram[pixelColor][d-1] + (double)sameColorNum / (double)totalBoundaryNum;
              }   
          } 
      }
  
      for(int c=0; c&lt;ColorBins; c++)
      {
          for(int d=0; d&lt;DistanceRange; d++)
          {
              if(pixelNum[c] != 0)
                  correlogram[c][d] = correlogram[c][d] / (double)pixelNum[c];
          }
      }
  }
  </code></pre>