title: Embedding a 3-dim cv::Mat in a 4-dim cv::Mat
body: <p>I have to embed a 3-dimensional <code>cv::Mat</code> into a 4-dimensional one.</p>
  
  <p>Some context</p>
  
  <p><strong>Given:</strong> I have some code performing a depthwise convolution, i.e. I have a 3-dim input matrix {channels, w_in, h_in} and a 3-dim 3-by3 kernel {channels, 3, 3} to produce a 3-dim output {channels, w_out, h_out}. Depthwise convolution now means that the first slice of the kernel is applied on the first slice of the input to produce the first slice of the output, and so on.</p>
  
  <p><strong>What I need:</strong> I want to rebuild this type of convolution using a standard convolution which needs a 4-dim kernel. In python (pytorch) I could do something like:</p>
  
  <pre class="lang-py prettyprint-override"><code>kernel4d = torch.tensor((), dtype=torch.float32)
  kernel4d = kernel4d.new_zeros((channels, channels, 3, 3))
  for i in range(channels):
      kernel4d[i, i, :, :] = kernel3d[i, :, :]
  </code></pre>
  
  <p>How can I re-write this code efficiently in C++ using <code>cv::Mat</code>?</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>int size3d[] = {3, 5, 5};
  cv::Mat kernel3d = cv::Mat(3, size3d, CV_32F, cv::Scalar(0));
  
  int size4d[] = {3, 3, 5, 5};
  cv::Mat kernel4d = cv::Mat(4, size4d, CV_32F, cv::Scalar(1));
  
  // Try 1
  for (int i = 0; i &lt; 3; i++){
      kernel4d.at&lt;float&gt;(i, i) = kernel3d.at&lt;float&gt;(i);
  }
  // Result: Sets only single values at (i, i, 0, 0)
  
  // Try 2
  for (int i = 0; i &lt; 3; i++){
      for (int k = 0; k &lt; 5; k++){
          for (int l = 0; l &lt; 5; l++){
              kernel4d.at&lt;float&gt;(i, i, k, l) = kernel3d.at&lt;float&gt;(i, k, l);
          }
      }
  }
  // Result: at does not exept 4 indexes
  </code></pre>