AJaramillo
AJaramillo

Reputation: 440

Python equivalent of matlab corr2

I want to know what is the python equivalent of the matlab function corr2 that gives the correlation coefficient between 2 matrices, return only one value.

http://www.mathworks.com/help/images/ref/corr2.html

I only found that the equivalent in python is scipy.signal.correlate2d but this returns an array.

Thanks.

Upvotes: 3

Views: 5555

Answers (2)

Jorge Rodriguez
Jorge Rodriguez

Reputation: 149

Maybe this can be help you

def mean2(x):
    y = np.sum(x) / np.size(x);
    return y

def corr2(a,b):
    a = a - mean2(a)
    b = b - mean2(b)

    r = (a*b).sum() / math.sqrt((a*a).sum() * (b*b).sum());
    return r

Upvotes: 6

brodoll
brodoll

Reputation: 1881

import numpy
print numpy.corrcoef(x,y)

Where x and y can be 1-d or 2-d like arrays.

Take a look at the docs here.

Upvotes: 0

Related Questions