# How can I multiply 3 matrices?

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
}
```

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

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