garak - 2 months ago 19

C++ Question

Is there any **built-in method** in OpenCV (**C++ API**) to **subtract two matrices row-wise**.

I have the following matrices:

`Mat A(10,2, CV_64F);`

Mat B(1,2, CV_64F);

Mat C(10,2, CV_64F);

C = A - B;

// B is a 1 x 2 matrix, A is a 10 x 2 matrix, and C is a 10 x 2 matrix.

Answer

I solved it the following way:

```
Mat A(10,2, CV_64F);
Mat B(1,2, CV_64F);
Mat C(0,2, CV_64F);
Mat D(0,2, CV_64F);
for(int i=0;i<A.rows;i++)
{
C=(A.row(i)-B.row(0));
D.push_back(C.row(0));
}
cout<<"A\n"<<A<<endl;
cout<<"B\n"<<B<<endl;
cout<<"C\n"<<C<<endl;
cout<<"D\n"<<D<<endl;
```