MATLAB: How to do correlation between each row of two matrices

correlationrows

I'm trying to calculation the correlation between each individual value in each row to the corresponding row in another matrix.
I have two matrices; both same size 442×1, 442×1.
I used the following code:
DR_all_stv = 442×1 matrix
Participation_Coeff = 442×1 matrix
stv_correlation = (corr(DR_all_stv(1:442,:),Participation_Coeff(1:442,:),'all',')');
However the corresponding code just gives me one correlation value, but I want to create an array that contains a value for each correlation between each row. It should be a 1 to 1 correlation.
Does anyone have any adivce on how I can calculate each correlation between the two corresponding rows?

Best Answer

The default correlation coefficient which corr calculates is described here (the Pearson linear correlation coefficient):
Wikipedia gives a nice alternative formula:
As Ameer implied, plugging scalar values into these equations will result in NaN. Sure enough,
>> corr(rand, rand)
ans =
NaN
What you can do is calculate a sliding window correlation between signals X and Y where the correlation between X(i) and Y(i) is really the correlation between windows of X and Y which are centered at i. This answer lays it out nicely. For example:
X = DR_all_stv;
Y = Participation_Coeff;
windowSize = 10; % result depends on window size you choose
% e.g. window size of 1 => eX = X, eY = Y, etc => corr is all NaN
eX = movmean(X, windowSize); % E[X], i.e. eX(i) is E[X(i-windowSize/2:i+windowSize/2)]
eXX = movmean(X.^2, windowSize); % E[X^2]
eY = movmean(Y, windowSize); % E[Y]
eYY = movmean(Y.^2, windowSize); % E[Y^2]
eXY = movmean(X.*Y, windowSize); % E[XY]
num = eXY - eX.*eY;
denom = sqrt(eXX - eX.^2) .* sqrt(eYY - eY.^2);
corr = num./denom;