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

33 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Ayush Modi
Ayush Modi am 28 Jul. 2024
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 Kommentare
albert Kinda
albert Kinda am 28 Jul. 2024
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 am 28 Jul. 2024
Bearbeitet: Torsten am 28 Jul. 2024
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 .

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by