title: Copy a matrix to a subset of another matrix
body: <p>I'm having difficulty copying a subset of a matrix into another larger matrix using OpenCV in C++.</p>
  
  <p>I tried the following code:</p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  
  void printMatrix(const cv::Mat &amp;M, std::string matrix)
  {
      printf("Matrix \"%s\" is %i x %i\n", matrix.c_str(), M.rows, M.cols);
      std::cout &lt;&lt; M &lt;&lt; std::endl;
  }
  
  int main(int argc, char ** argv)
  {
      cv::Mat P0(3, 4, CV_32F);
      printMatrix(P0, "P0 Initial");
  
      cv::Mat R0 = cv::Mat::eye(3,3,CV_32F);
      printMatrix(R0, "R0 I");
  
      R0.copyTo(P0.colRange(0,2));
      printMatrix(P0, "P0 with R");
  
      return 0;
  }
  </code></pre>
  
  <p>Which produces the following output:</p>
  
  <pre><code>Matrix "P0 Initial" is 3 x 4
  [-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
  -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
  -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008]
  
  Matrix "R0 I" is 3 x 3
  [1, 0, 0;
  0, 1, 0;
  0, 0, 1]
  
  Matrix "P0 with R" is 3 x 4
  [-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
  -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
  -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008]
  </code></pre>
  
  <p>While suggests that the copy operation is not doing anything.</p>
  
  <p>I found a similar post <a href="https://stackoverflow.com/questions/4031233/in-opencv-2-1-how-to-assign-a-matrix-to-a-submatrix-of-another-matrix">here</a> but updating the relevant line to the following as suggested in that post still produces the same output.</p>
  
  <pre><code>//R0.copyTo(P0.colRange(0,2));
  cv::Mat dest = P0.colRange(0,2);
  printMatrix(P0, "P0 with R");
  </code></pre>