matrixR2.m (565B)
1 function R2=matrixR2(A,B); 2 % function []=matrixR2(A,B); 3 % 4 % Given two matrices, A and B, of the same dimensions, compute an R2-like 5 % measure of how similar they are: 6 % 7 % R2=1-RSS/TSS, where RSS = sum of squared deviations (element by element) 8 % and TSS = total sum of squares of each element 9 % 10 % Like SUM((a_ij-b_ij)^2) / SUM(a_ij ^2+b_ij^2) 11 % 12 % Note, we first take out the mean value for each matrix. 13 14 a=demean(vector(A)); b=demean(vector(B)); % Stack each matrix into a vector 15 RSS=sum((a-b).^2); 16 TSS=sum(a.^2+b.^2); 17 R2=1-RSS/TSS;