Bonjour Tout Le Monde,
J'essaie d'écrire un programme qui calcule la matrice de covariance et affiche le résultat. Au moment de l'exécution du code, le programme plante. J'ai un doute sur la définition des variables mais je ne sais pas où exactement.
Voici mes deux fonctions qui calculent la moyenne et la covariance :
Et pour afficher le résultat (après avoir cliqué sur un bouton), voici la fonction :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 bool Gadp::ComputeMeanColumnVector(std::vector<double> &o_vMean ) { o_vMean.clear(); if(m_Matrix.size() != m_iNBColumn || m_Matrix[0].size() != m_iNBRow) return false; for(int i = 0; i < m_iNBColumn; i++) { double somme = 0; for(int j = 0; j < m_iNBRow ; j++) { somme += m_Matrix[i][j]; } double mean = somme/m_iNBRow; o_vMean.push_back(mean); } return true; } const std::vector<std::vector<double> > Gadp::GetCovariance() { return m_var; } // Cov(i,j) = sum[ (X-mean(Xi))* (Y - mean(Yj))] / n void Gadp::calCovariance() { int i,j,k; double factor; ComputeMeanColumnVector(m_MeanMat); for(i=0;i<m_iNBRow; i++) { for(j=0;j<m_iNBColumn; j++) { factor=0; for(k=0;k<m_iNBRow;k++) { factor+=(m_mat[k][i] - m_MeanMat[i])*(m_mat[k][j] - m_MeanMat[j]); } m_var[i][j]= factor/m_iNBColumn; } } return ; }
Quelqu'un aurait-il une idée s'il vous plait ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 void CInterfaceDlg::OnBnClickedButton1(void) { std::vector<double> l_vMean; std::vector<std::vector<double> > l_coVar; m_gadp.calCovariance(); l_coVar=m_gadp.GetCovariance(); CString strText; CString aAfficher = _T(""); CString temp; for( int i=0; i<l_coVar.size(); i++ ) { for( int j=0; j<l_coVar[i].size(); j++ ) { temp.Format("%.2f ", l_coVar[i][j]); aAfficher += temp; } aAfficher += _T("\r\n"); } AfxMessageBox(aAfficher); int i = m_gadp.GetNBRows(); // Insert the item, select every other item. theCtrl.InsertItem(LVIF_TEXT|LVIF_STATE,i, strText,(i%2)==0?LVIS_SELECTED : 0, LVIS_SELECTED,0, 0); // Initialize the text of the subitems. for (unsigned int j=0;j<l_vMean.size();j++) { std::ostringstream l_oss; l_oss << l_vMean[j]; strText.Format(TEXT("%s"), l_oss.str().c_str()); theCtrl.SetItemText(i, j+1, strText); } }
Merci par avance.
Partager