필터 지우기
필터 지우기

How to do correlation between each row of two matrices?

조회 수: 10 (최근 30일)
Natasha Taylor
Natasha Taylor 2020년 5월 28일
편집: Tommy 2020년 5월 28일
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 442x1, 442x1.
I used the following code:
DR_all_stv = 442x1 matrix
Participation_Coeff = 442x1 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?
  댓글 수: 1
Ameer Hamza
Ameer Hamza 2020년 5월 28일
Rows of DR_all_stv and Participation_Coeff have only one element. How do you want to calculate corr() between two scalars? Correlation is defined for multiple data points.

댓글을 달려면 로그인하십시오.

채택된 답변

Tommy
Tommy 2020년 5월 28일
편집: Tommy 2020년 5월 28일
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;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by