# Efficient calculation with multiple cv::Mat

Hello, I need to do some math with my matrices and I would like to know the most performant way. I have the following vectors:

```
std::vector<cv::Mat> b //200 matrices with 1 row and 1000 elements per row
std::vector<cv::Mat> w //200 matrices with 1 row and 1000 elements per row
std::vector <cv::Mat>image //200 matrices with 500 rows and 1000 elements per row
```

All vectors with the same index belong together. I need to calculate the following for every Mat of the vectors with the same index and every pixel in the image:

```
image(x, y) = (image(x,y) – b(x)) / (w(x) – b(x)) * 0,9
```

I would start like the following:

```
for(int i=0; i<image.size(); i++)
{
//And here?
//Usage of the “at”-method or pointer arithmetic?
//Or is there an operation which can calculate everything automatically line by line?
}
```

Thank you very much :-)

you could precalculate Mat mwb = 0.9 / (w - b), which is used to scale the results of every line, then for each row of an image you could calculate new_image_row = mwb * (image_row - b), where '*' is an element by element multiply. Parallelism (CPU core or thread) is a possible thing to exploit, see TBB and threads. SIMD vector arithmetic might allows you to accelerate the cell arithmetic for specific Mat data cell types. Good luck!

Thanks for the idea! I just had another one. I could make the number of rows of "b" and "w" dynamically the same as image and then use direct methods like "cv::subtract". Is this a good idea?