How can I multiply 3 matrices?

asked 2014-02-21 20:54:37 -0600

Nihad gravatar image

I would like to multiply 3 matrices(same size). For eample, NM=(-0.5)HSH where H,S are matrices having same size. Here is my code, but it produces nan output. How can i multiply 3 matrices?

int main()
{
.......................
int count_row=1000;
 vector <vector <float> >g;
//do some calculation on g

Mat M(count_row,count_row,CV_32F);
for(int i=0;i<count_row;i++)
{
     for(int j=0;j<count_row;j++)
     {
         M.at<float>(i,j)=g[i][j];

     }
}

Mat I = Mat::eye(count_row, count_row, CV_32F);

Mat H(count_row,count_row,CV_32F);
H=I-1.0/(float)count_row;

Mat S(count_row,count_row,CV_32F);
pow(M,2.0,S);

Mat normalized_M(count_row,count_row,CV_32F);
normalized_M=(-0.5)*H*S*H;

cout<<"normalized_M:"<<endl;
cout<<normalized_M<<endl;// error: produce nan output
}
edit retag flag offensive close merge delete

Comments

Put an isnan(g[i][j]) check in your for loop.

Nghia gravatar imageNghia ( 2014-02-22 07:36:26 -0600 )edit

you can also make use of overloading of operators as done in c++ programming by creating an overloaded * operator function.

Abhishek Kumar Annamraju gravatar imageAbhishek Kumar Annamraju ( 2014-02-26 02:34:08 -0600 )edit