How to do correlation between each row of two matrices?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Natasha Taylor
el 28 de Mayo de 2020
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 comentario
Ameer Hamza
el 28 de Mayo de 2020
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.
Respuesta aceptada
Tommy
el 28 de Mayo de 2020
Editada: Tommy
el 28 de Mayo de 2020
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Surface and Mesh Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!