Hello,

I have a major problem with a Correlation algorithm I'm trying to implement..

Basically, when I have two small matices:
Matrix 1:
0 1 0 1 1 1
1 1 1 1 1 1
0 1 0 0 0 1
1 1 0 0 0 0
1 1 1 1 1 1
0 0 0 0 0 0

Matrix2:
0 1 0 1 1 1
1 1 1 1 1 1
0 1 0 0 0 1
1 1 0 0 0 0
1 1 1 1 1 1
0 0 0 0 0 0

The result would be: 0.00142383

If I changed Matrix 2:

0 0 0 0 0 0
1 1 1 1 1 1
0 0 0 0 0 0
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0

The result would be: 0.000263672

This is correct and works perfectly.. But I'm working on 2 512x512 matrices and when I try to find the Correlation it produces a number like: 1.64643e-14 which is obviously wrong.. I have tried everything.. Here is the algorithm I was given:

-Find the mean value of both matrices; subtract each element of
the matrices by its mean value
-For the numerator, multiply matrices element by element; take
the sum of the resultant matrix after multiplication
- For the denominator,square each matrix element by element;
take the sum of each squared matrix; multiply the sum; square
the multiplied results
- Divide the numerator by the denominator

The larger the similarity score, the more similar the matrices are

And here is my algorithm:

double Matrix::getMean(double* theMatrix)
{
    double add = 0;
    double mean = 0;
    
    for(int i=0; (i < rows); i++)
    {
        for(int j=0; (j < columns); j++)
        {
            add += theMatrix[i*rows+j];
        }
    }
    
    mean = add/(rows*columns);
    
    return mean;
}
double Matrix::getSum(double *theMatrix)
{
    double sum = 0;
    for(int i=0; (i < columns); i++)
    {
        for(int j=0; (j < rows); j++)
        {
            sum += theMatrix[i*rows+j];
        }
    }
    return sum;
    
}

double* Matrix::squareMatrix(double* theMatrix)
{
    for(int i=0; (i < rows); i++)
    {
        for(int j=0; (j < columns); j++)
        {
            const int transformedIndex = i * rows + j;
            theMatrix[transformedIndex] = theMatrix[transformedIndex]*theMatrix[transformedIndex];
        }
    }
    return theMatrix;
}


double Matrix::Correlation(double* matrix1, double* matrix2)
{

    double Matrix1Mean = getMean(matrix1);
    double Matrix2Mean = getMean(matrix2);
    double* matrix3 = new double[rows*columns];
    
    for(int i=0; (i < columns); i++)
    {
        for(int j=0; (j < rows); j++)
        {        
            matrix1[i*rows+j] -= Matrix1Mean;
            matrix2[i*rows+j] -= Matrix2Mean;
        }        
    }
    for(int i=0; (i < rows); i++)
    {
        for(int j=0; (j < columns); j++)
        {
            matrix3[i*rows+j] = matrix1[i*rows+j]*matrix2[i*rows+j];
        }
    }
    //cout << matrix1[20] << endl;
    double* squareMatrix1 = squareMatrix(matrix1);
    double* squareMatrix2 = squareMatrix(matrix2);
    
    double sum1 = getSum(matrix1);
    double sum2 = getSum(matrix2);
    
    double denominator = pow((sum1*sum2),2);
    
    double numerator = getSum(matrix3);  
    
    double final = numerator/denominator;
    
    cout << final;
   
}

Does anyone have any ideas? Thanks :)

Edited 4 Years Ago by phorce: n/a

But I'm working on 2 512x512 matrices and when I try to find the Correlation it produces a number like: 1.64643e-14 which is obviously wrong.. I have tried everything..

What is obviously wrong about it?

It produces an unreadable number "1.64643e-14" because the two numbers are too big?

This question has already been answered. Start a new discussion instead.