123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <html> <head>
- <title>MATLAB to Eigen</title>
- <style type="text/css">
- table
- {
- border-spacing: 0px;
- }
- tr.header th
- {
- border-bottom: 1px solid;
- background-color: #ffb;
- padding-left: 10px;
- padding-right: 20px;
- }
- tr.d0 td
- {
- background-color: #EEE;
- color: black;
- padding-left: 10px;
- padding-right: 20px;
- min-width: 200px;
- }
- tr.d1 td
- {
- background-color: #bcf;
- color: black;
- padding-left: 10px;
- padding-right: 20px;
- min-width: 200px;
- }
- tr.gotcha1 td
- {
- background-color: #fcb;
- color: black;
- padding-left: 10px;
- padding-right: 20px;
- min-width: 200px;
- }
- tr.gotcha2 td
- {
- background-color: #fed;
- color: black;
- padding-left: 10px;
- padding-right: 20px;
- min-width: 200px;
- }
- </style>
- </head>
- <body>
- <table>
- <tr class="header">
- <th>MATLAB</th>
- <th>Eigen</th>
- <th>Notes</th>
- </tr>
- <tr class=d0>
- <td><pre><code>[Y,IX] = sort(Y,dim,mode)</code></pre></td>
- <td><pre><code>igl::sort(X,dim,mode,Y,IX)</code></pre></td>
- <td>MATLAB version allows Y to be a multidimensional matrix, but the
- Eigen version is only for 1D or 2D matrices.</td>
- </tr>
- <tr class=d1>
- <td><pre><code>B(i:(i+w),j:(j+h)) = A(x:(x+w),y:(y+h))</code></pre></td>
- <td><pre><code>B.block(i,j,w,h) = A.block(i,j,w,h)</code></pre></td>
- <td>MATLAB version would allow w and h to be non-positive since the
- colon operator evaluates to a list of indices, but the Eigen version
- needs non-negative width and height values.</td>
- </tr>
- <tr class=d0>
- <td><pre><code>max(A(:))</code></pre></td>
- <td><pre><code>A.maxCoeff()</code></pre></td>
- <td>Find the maximum coefficient over all entries of the matrix.</td>
- </tr>
- <tr class=d1>
- <td><pre><code>min(A(:))</code></pre></td>
- <td><pre><code>A.minCoeff()</code></pre></td>
- <td>Find the minimum coefficient over all entries of the matrix.</td>
- </tr>
- <tr class=d0>
- <td><pre><code>eye(w,h)</code></pre></td>
- <td><pre><code>MatrixXd::Identity(w,h), MatrixXf::Identity(w,h), etc.</code></pre></td>
- <td></td>
- </tr>
- <tr class=d1>
- <td><pre><code>A(i:(i+w),j:(j+h)) = eye(w,h)</code></pre></td>
- <td><pre><code>A.setIdentity()</code></pre></td>
- <td></td>
- </tr>
- <tr class=d0>
- <td><pre><code>[I,J,V] = find(X)</code></pre></td>
- <td><pre><code>igl::find(X,I,J,V)</code></pre></td>
- <td>Matlab supports finding subscripts (I and J) as well as indices
- (just I), but so far igl::find only supports subscripts. Also,
- igl::find requires X to be sparse.</td>
- </tr>
- <tr class=d1>
- <td><pre><code>X(:,j) = X(:,j) + x</code></pre></td>
- <td><pre><code>X.col(j).array() += x</code></pre></td>
- <td></td>
- </tr>
- <tr class=d0>
- <td><pre><code>Acol_sum = sum(A,1)<br>Arow_sum = sum(A,2)<br>Adim_sum = sum(Asparse,dim)</code></pre></td>
- <td><pre><code>Acol_sum = A.colwise().sum()<br>Arow_sum = A.rowwise().sum()<br>igl::sum(Asparse,dim,Adim_sum)</code></pre></td>
- <td>Currently the igl version only supports sparse matrix input (and
- dim must be 1 or 2)</td>
- </tr>
- <tr class=d1>
- <td><pre><code>D = diag(M)</code></pre></td>
- <td><pre><code>igl::diag(M,D)</code></pre></td>
- <td>Extract the main diagonal of a matrix. Currently igl version
- supports sparse only.</td>
- </tr>
- <tr class=d0>
- <td><pre><code>M = diag(D)</code></pre></td>
- <td><pre><code>igl::diag(D,M)</code></pre></td>
- <td>Construct new square matrix M with entries of vector D along the
- diagonal. Currently igl version supports sparse only.</td>
- </tr>
- <tr class=d1>
- <td><pre><code>[Y,I] = max(X,[],dim)</code></pre></td>
- <td id=mat_max><pre><code>igl::mat_max(X,dim,Y,I)</code></pre></td>
- <td>Matlab has a bizarre convention of passing [] as the second
- argument to mean take the max/min along dimension dim.</td>
- </tr>
- <tr class=d0>
- <td><pre><code>Y = max(X,[],1)<br>Y = max(X,[],2)<br>Y = min(X,[],1)<br>Y = min(X,[],2)</code></pre></td>
- <td><pre><code>Y = X.colwise().maxCoeff()<br>Y = X.rowwise().maxCoeff()<br>Y = X.colwise().minCoeff()<br>Y = X.rowwise().minCoeff()</code></pre></td>
- <td>Matlab allows you to obtain the indices of extrema see <a href=#mat_max>mat_max</a></td>
- </tr>
- <tr class=d1>
- <td><pre><code>C = A.*B</code></pre></td>
- <td><pre><code>C = (A.array() * B.array()).matrix()</code></pre></td>
- <td></td>
- </tr>
- <tr class=d0>
- <td><pre><code>C = A.^b</code></pre></td>
- <td><pre><code>C = A.array().pow(b).matrix()</code></pre></td>
- <td></td>
- </tr>
- <tr class=d1>
- <td><pre><code>A(B == 0) = C</code></pre></td>
- <td><pre><code>A = (B == 0).select(C,A)</code></pre></td>
- <td></td>
- </tr>
- <tr class=gotcha1>
- <td><pre><code>C = A + B'</code></pre></td>
- <td><pre><code>SparseMatrixType BT = B.transpose()<br>SparseMatrixType C = A+BT;</code></pre></td>
- <td>Do <b>not</b> attempt to combine .transpose() in expression like
- this: <pre><code>C = A + B.transpose()</code></pre></td>
- </tr>
- <tr class=gotcha2>
- <td><pre><code>[L,p] = chol(A)</code></pre></td>
- <td><pre><code>SparseLLT<SparseMatrixType> A_LLT(A.template triangularView<Lower>())<br>SparseMatrixType L = A_LLT.matrixL();bool p = (L*0).eval().nonZeros()==0;</code></pre></td>
- <td>Do <b>not</b> attempt to use A in constructor of A_LLT like
- this: <pre><code>SparseLLT<SparseMatrixType> A_LLT(A)</code></pre><br>
- Do <b>not</b> attempt to use A_LLT.succeeded() to determine if Cholesky
- factorization succeeded, like this:
- <pre><code>bool p = A_LLT.succeeded()</code></pre>
- </td>
- </tr>
- <tr class=d0>
- <td><pre><code>X = U\(L\b)</code></pre></td>
- <td><pre><code>X = b;<br>L.template triangularView<Lower>().solveInPlace(X);<br>U.template triangularView<Upper>().solveInPlace(X);</code></pre></td>
- <td>Expects that L and U are lower and upper triangular matrices
- respectively</td>
- </tr>
- <tr class=d1>
- <td><pre><code>B = repmat(A,i,j)</code></pre></td>
- <td><pre><code>igl::repmat(A,i,j,B)</code></pre></td>
- <td>So far igl::repmat is only implemented for dense matrices.
- </td>
- </tr>
- <tr class=d0>
- <td><pre><code>I = low:step:hi</code></pre></td>
- <td><pre><code>igl::colon(low,step,hi,I)</code></pre></td>
- <td>IGL version should be templated enough to handle same situations as
- matlab's colon. The matlab keyword <b>end</b> does not correspond in
- the C++ version. You'll have to use M.size(),M.rows() or whatever.
- </td>
- </tr>
- <tr class=d1>
- <td><pre><code>O = ones(m,n)</code></pre></td>
- <td><pre><code>Matrix* O = Matrix*::Ones(m,n)</code></pre></td>
- <td></td>
- </tr>
- <tr class=d0>
- <td><pre><code>O = zeros(m,n)</code></pre></td>
- <td><pre><code>Matrix* O = Matrix*::Zero(m,n)</code></pre></td>
- <td></td>
- </tr>
- <tr class=d1>
- <td><pre><code>B = A(I,J)<br>B = A(I,:)</code></pre></td>
- <td><pre><code>igl::slice(A,I,J,B)<br>B = igl::slice(A,I,igl::colon(0,A.cols()-1))</code></pre></td>
- <td></td>
- </tr>
- <tr class=d0>
- <td><pre><code>B(I,J) = A<br>B(I,:) = A</code></pre></td>
- <td><pre><code>igl::slice_into(A,I,J,B)<br>B = igl::slice_into(A,I,igl::colon(0,B.cols()-1))</code></pre></td>
- <td></td>
- </tr>
- <tr class=d1>
- <td><pre><code>M = mode(X,dim)</code></pre></td>
- <td><pre><code>igl::mode(X,dim,M)</code></pre></td>
- <td></td>
- </tr>
- <tr class=d0>
- <td><pre><code>A = arrayfun(FUN, B)</code></pre></td>
- <td><pre><code>B = A.unaryExpr(ptr_fun(FUN))</code></pre></td>
- <td>If FUN is templated, the templates must be fully resolved.</td>
- </tr>
- <!-- Insert rows for each command pair -->
- <!-- Leave this here for copy and pasting
- <tr class=d0>
- <td><pre><code>Matlab code</code></pre></td>
- <td><pre><code>Eigen code</code></pre></td>
- <td>Notes</td>
- </tr>
- -->
- </table>
- <a href="http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt">Eigen's
- "ASCII Quick Reference" with MATLAB translations</a>
- </body>
- </html>
|