# cv::Mat divide by last row.

Hi. How can I write a OpenCV code for following.

if there is a 3xN vector, divide all elements by last row.

cv::Mat divide by last row.

Hi. How can I write a OpenCV code for following.

if there is a 3xN vector, divide all elements by last row.

add a comment

1

Hi,

I don't know if it is particularly efficient but you can do the following using repeat

```
Mat src ...; // your 3XN matrix
Mat lastRow = src.row( 2 );
Mat tmp;
repeat(lastRow, tmp, 3, 1 ); // create a tmp matrix of 3xN whose elements are repeated elements of the last row of src
src = src / tmp;
```

This is like doing

```
src = src./repmat(src(3,:), 3, 1)
```

in matlab. Otherwise another way to do it is to pass element by element and do the division

```
for(int i =0; i<src.cols; ++i )
{
// you should first check that the last element is !=0 !!!
src.at<float>(0, i) /= src.at<float>(2, i);
src.at<float>(1, i) /= src.at<float>(2, i);
}
```

and replace <float> with the correct type of your matrix.

Hope it helps.

S.

Asked: **
2012-11-08 11:08:13 -0500
**

Seen: **1,198 times**

Last updated: **Nov 08 '12**

GpuMat submatrix out of GpuMat object?

multiply two points and matrix

different step size output for cv::Mat::step1

How to operate on each pixel of a cv2.cv.Mat with a 3 x 3 matrix?

missing cv::Mat::zeros(int ndims, const int* sz, int type)

cv::sum() and cv::norm() for CV_U8...

Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.