How to do correlation between each row of two matrices?

3 ビュー (過去 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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by