Efficient matrix copying in OpenCV

opencv

Solution

You should take a look at `Mat::copyTo` and `Mat::clone`. `copyTo` will make an copy with optional mask where its non-zero elements indicate which matrix elements need to be copied.

mk.copyTo(sk, nz);

And if you really want a row matrix then call `sk.reshape()` as member sansuiso already suggested. This method ...

creates alternative matrix header for the same data, with different number of channels and/or different number of rows.

Problem

I have no idea for how to implement matrix implementation efficiently in OpenCV. I have binary `Mat nz(150,600)` with 0 and 1 elements. I have Mat `mk(150,600)` with double values. I like to implement as in Matlab as ``` sk = mk(nz); ``` That command copy mk to sk only for those element of mk element at the location where nz has 1. Then make sk into a row matrix. How can I implement it in OpenCV efficiently for speed and memory?

Original source