Home > atmlab > math > chi2.m

chi2

PURPOSE ^

CHI2 Chi2 distance

SYNOPSIS ^

function c = chi2(V,S)

DESCRIPTION ^

 CHI2   Chi2 distance

    Calculates:
      v' * inv(S) * v
    where v is a vector and S is the associated covariance matrix.

 FORMAT   c = chi2(V,S)
        
 OUT   c   Distance value
 IN    V   Data matrix, where each column corresponds to v above.
       S   Covariance matrix

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

chi2.m

SOURCE CODE ^

0001 % CHI2   Chi2 distance
0002 %
0003 %    Calculates:
0004 %      v' * inv(S) * v
0005 %    where v is a vector and S is the associated covariance matrix.
0006 %
0007 % FORMAT   c = chi2(V,S)
0008 %
0009 % OUT   c   Distance value
0010 % IN    V   Data matrix, where each column corresponds to v above.
0011 %       S   Covariance matrix
0012 
0013 % 2005-11-23   Created by Patrick Eriksson.
0014 
0015 function c = chi2(V,S)
0016 
0017 
0018 if size(V,1) ~= size(S,1)
0019   error( 'Mismatch in size between V and S.' );
0020 end
0021 
0022 
0023 %= If S is diagonal, a faster expression can be used
0024 
0025 if length(find(S)) == size(S,1)
0026   %
0027   c = sum( V.^2 .* repmat(1./diag(S),1,size(V,2) ) )';
0028 
0029 else
0030   %
0031   Sinv = inv( S );
0032   c = sum( V .* ( Sinv * V ) )';
0033 
0034 end
0035 
0036 return
0037 
0038 % The last calculation above is equal to:
0039 %
0040 %   for i = 1 : size(V,2)
0041 %     c(i) = V(:,i)'*Sinv*V(:,i);
0042 %   end
0043 %
0044 % but is much faster than this later solution.

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005