template<class ArrayT>
Covariance matrix. Compute the covariance matrix of the observed data Output: [nc x nc] covariance matrix and (optionally) a vector of mean-values for each dimension. This version is exact, also for a small number of rows in m. Straightforward implementation, so slower than the other version Definition at line 88 of file MatCovariance.h. References MatDiv(), MatE(), MatNrCol(), MatNrRow(), MatSet(), and VecE(). Referenced by Impala::Core::Tracking::Classifier::SetBackground(). 00089 { 00090 typedef typename ArrayT::ArithType ArithT; 00091 typedef typename ArrayT::StorType StorT; 00092 00093 int nr = MatNrRow(m); 00094 int nc = MatNrCol(m); 00095 ArrayT* cov = MatCreate<ArrayT>(nc, nc); 00096 MatSet(cov, 0); 00097 ArrayT* mean = VecCreate<ArrayT>(nc) ; //means 00098 MatSet(mean, 0); 00099 00100 int k; 00101 for ( k=0; k<nr; k++) 00102 { 00103 StorT* meanPtr = VecE(mean, 0); 00104 StorT* mkPtr = MatE(m, k, 0); 00105 for (int i=0; i<nc; i++) 00106 { 00107 ArithT mkiVal = mkPtr[i]; 00108 meanPtr[i] += mkiVal; 00109 /* 00110 StorT* coviPtr = MatE(cov, i, 0); 00111 for (int j=i; j<nc; j++) { 00112 coviPtr[j] += mkiVal * mkPtr[j]; 00113 } 00114 */ 00115 } 00116 } 00117 00118 MatDiv(mean, nr); 00119 00120 for ( k=0; k<nr; k++) { 00121 StorT* meanPtr = VecE(mean, 0); 00122 StorT* mkPtr = MatE(m, k, 0); 00123 for (int i=0; i<nc; i++) 00124 { 00125 //ArithT mkiVal = mkPtr[i]; 00126 StorT* coviPtr = MatE(cov, i, 0); 00127 for (int j=i; j<nc; j++) 00128 { 00129 coviPtr[j] += (mkPtr[i] - meanPtr[i]) * (mkPtr[j] - meanPtr[j]); 00130 } 00131 } 00132 } 00133 00134 for (int i=0; i<nc; i++) { 00135 StorT* coviPtr = MatE(cov, i, 0); 00136 //StorT* meanPtr = VecE(mean, 0); 00137 //ArithT meani = meanPtr[i]; 00138 for (int j=i; j<nc; j++) 00139 { 00140 ArithT res = coviPtr[j] / (nr - 1); 00141 coviPtr[j] = res; 00142 *MatE(cov, j, i) = res; 00143 } 00144 } 00145 00146 if (meanOut != 0) 00147 MatSet(meanOut, mean); 00148 delete mean; 00149 00150 return cov; 00151 }
Here is the call graph for this function:
|