title: Eigen3: defining a variable size matrix
body: <p>I want to write a routine to create the transpose of a matrix as a row major double matrix using <em>Eigen3</em> (inside <em>RcppEigen</em>). In the main I will use it for matrices of different size. </p>
  
  <p>In Eigen3 the template <code>Matrix</code> takes 6 arguments: </p>
  
  <ul>
  <li><p>Three are compulsory: <br/>
  <em>(Scalar, RowsAtCompileTime and ColsAtCompileTime)</em> </p></li>
  <li><p>Three are optional:<br/>
   <em>(Options, MaxRowsAtCompileTime and MaxColsAtCompileTime)</em> <br/>
   <em>Options</em> can be <em>RowMajor</em> or <em>ColMajor</em></p></li>
  </ul>
  
  <p>I prefer to dynamically assign the dimensions to matrices because I work with large datasets and I want to avoid wasting RAM.</p>
  
  <p>So, I wrote the following</p>
  
  <pre><code>Eigen::MatrixXd mktrasp(Eigen::MatrixXd X const int n, const int p){
    Eigen::Matrix&lt;double, p, n, RowMajor&gt; T;
    T = X.transpose();
    return T;
  }
  </code></pre>
  
  <p>But I get the compiler errors</p>
  
  <blockquote>
    <p>error: 'p' cannot appear in a constant-expression<br/>
    error: 'n' cannot appear in a constant-expression</p>
  </blockquote>
  
  <p>I imagine this happens because <code>n</code> and <code>p</code> are not known at compile time. </p>
  
  <p>So I tried this solution</p>
  
  <pre><code>Eigen::MatrixXd mktrasp(Eigen::MatrixXd X, const int n, const int p){
    Eigen::Matrix&lt;double, Dynamic, Dynamic, RowMajor&gt; T(p, n);
    T = X.transpose();
    return T;
  }
  </code></pre>
  
  <p>This works but I have completely lost the plot. Is <code>T</code> a dynamic matrix or fixed size now? Is it safe to do this? Can someone suggest a better approach? </p>