How to calculate the determination coefficient R^2 with two matrix ?

33 次查看(过去 30 天)
hello I have two matrices A(i,j) and B (i,j). How to calculate for each i the covariance (A,B)? also calculate the determination coefficientR ^2?

采纳的回答

Ayush Modi
Ayush Modi 2024-7-28
Hi Albert,
You can calculate the covariance between two matrices A and B using "cov" function. To calculate covariance for each row, iterate over each row using a loop. Here is the sample code:
A = [1 9 3; 4 5 6; 7 8 6]; % Replace with your matrix A
B = [6 8 7; 6 5 4; 3 2 1]; % Replace with your matrix B
[numRows, numCols] = size(A);
covarianceValues = zeros(numRows, 1);
R2Values = zeros(numRows, 1);
% Loop through each row
for i = 1:numRows
% Extract the i-th row from A and B
Ai = A(i, :);
Bi = B(i, :);
covMatrix = cov(Ai, Bi);
covarianceValues(i) = covMatrix(1, 2);
To calculate the determination coefficient R^2, you can calculate the standard deviation of each row using "std" function and apply the formula for 'r'.
sigmaA = std(Ai);
sigmaB = std(Bi);
% Calculate the determination coefficient R^2
R2Values(i) = (covarianceValues(i) / (sigmaA * sigmaB))^2;
end
disp(covarianceValues);
4.0000 -1.0000 0.5000
disp('Determination coefficient R^2 for each row:');
Determination coefficient R^2 for each row:
disp(R2Values);
0.9231 1.0000 0.2500
Note - covariance matrix covMatrix returned by the cov function contains the variances of Ai and Bi on the diagonal and the covariance between Ai and Bi off the diagonal.
Refer to the following documentation for more information on the function:
  4 个评论
albert Kinda
albert Kinda 2024-7-28
The correlation coefficient is the covariance divided by the two standard deviations marginal
The determination coefficient is the square of the correlation coefficient
Torsten
Torsten 2024-7-28
编辑:Torsten 2024-7-28
You asked for RMSE in your comment, not for the correlation coefficient or the coefficient of determination.
Better you just write down the mathematical formula of what you want if a, b are vectors of the same size. This will avoid confusion about terminology .

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by