index

// Gray Code Measure of Similarity == XOR

typedef unsigned int uint;

uint XORGrayCodeSimilarity(vector<bool> vGray1, vector<bool> vGray2);
uint XORGrayCodeSimilarityBinaryCodeFormat(vector<bool> vBinary1, vector<bool> vBinary2);

uint XORGrayCodeSimilarity(vector<bool> vGray1, vector<bool> vGray2)   // Gray
{  // Vectors must be in gray code format - not binary code format. Smaller is closer or more similiar
       
       
uint nSim = vGray1.size();

        if(vGray1.size() == vGray2.size())
        {
                nSim = 0;
                for(uint n = 0; n < vGray1.size(); n++)
                {
                        if(vGray1[n] != vGray2[n])
                        {
                                nSim++; // XOR; and smaller is better
                        }
                }
        }
        return nSim;
}

uint XORGrayCodeSimilarityBinaryCodeFormat(vector<bool> vBinary1, vector<bool> vBinary2)
{    // Here both vectors are in binary code format; it gives the same result if both vectors were changed to gray code format and the above function used

        uint nSim = vBinary1.size();

        if(vBinary1.size() == vBinary2.size())
        {
            nSim = 0;
            for(uint n = 0; n < vBinary1.size(); n++)
            {
                 if(nSim % 2 == 0)
                 {
                      if(!vBinary2[n] && vBinary1[n])
                       {
                           nSim++;                                 // sim 0, x 0, y 1
                       }
                       if( vBinary2[n] && !vBinary1[n])
                       {
                           nSim++;                                 // sim 0, x 1, y 0
                       }
                  }
                  if(nSim % 2 == 1)
                  {
                       if(vBinary2[n] && vBinary1[n])
                        {
                            nSim++;                                 // sim 1, x 1, y 1
                        }
                        if(!vBinary2[n] && !vBinary1[n])
                        {
                            nSim++;                                 // sim 1, x 0, y 0
                        }
                  }
            }
        }

        return nSim;
}