123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <html> <head>
- <title>MATLAB to Eigen</title>
- <link href="./style.css" rel="stylesheet" type="text/css">
- </head>
- <body>
- <table>
- <tr class="header">
- <th>MATLAB</th>
- <th>Eigen with libigl</th>
- <th>Notes</th>
- </tr>
- <tr class=d0>
- <td><pre><code>[Y,IX] = sort(X,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.block(i,j,w,h).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(B==0)</code></pre></td>
- <td><pre><code>A = (B.array() == 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);
- B = A.replicate(i,j);</code></pre></td>
- <td>igl::repmat is also implemented for Sparse 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);
- // or
- const int size = ((hi-low)/step)+1;
- I = VectorXiLinSpaced(size,low,low+step*(size-1));</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>This is only for the case when I and J are lists of indices and
- not vectors of logicals.</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>This is only for the case when I and J are lists of indices and
- not vectors of logicals.</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>B = arrayfun(FUN, A)</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>
- <tr class=d1>
- <td><pre><code>B = fliplr(A)<br>B = flipud(A)</code></pre></td>
- <td><pre><code>B = A.rowwise().reverse().eval()<br>B =
- A.colwise().reverse().eval()</code></pre></td>
- <td>The <code>.eval()</code> is not necessary if A != B</td>
- </tr>
- <tr class=d0>
- <td><pre><code>B = IM(A)
- A = IM(A);
- </code></pre></td>
- <td><pre><code>B = A.unaryExpr(bind1st(mem_fun(
- static_cast<VectorXi::Scalar&(VectorXi::*)(VectorXi::Index)>
- (&VectorXi::operator())), &IM)).eval();
- // or
- for_each(A.data(),A.data()+A.size(),[&IM](int & a){a=IM(a);});
- </code></pre></td>
- <td>Where IM is an "index map" column vector and A is an arbitrary
- matrix. The <code>.eval()</code> is not necessary if A != B, but then
- the second option should be used.</td>
- </tr>
- <tr class=d1>
- <td><pre><code>A = sparse(I,J,V)</code></pre></td>
- <td><pre><code>// build std::vector<Eigen::Triplet> IJV
- A.setFromTriplets(IJV);
- </code></pre></td>
- <td>IJV and A should not be empty! (this might be fixed)</td>
- </tr>
- <tr class=d0>
- <td><pre><code>A = min(A,c);</code></pre></td>
- <td><pre><code>C.array() = A.array().min(c);
- </code></pre></td>
- <td>Coefficient-wise minimum of matrix and scalar (or matching size matrix)</td>
- </tr>
- <tr class=d1>
- <td><pre><code>I=1:10;
- ...
- IP = I(P==0);</code></pre></td>
- <td><pre><code>I = VectorXi::LinSpaced(10,0,9);
- ...
- VectorXi IP = I;
- IP.conservativeResize(stable_partition(
- IP.data(),
- IP.data()+IP.size(),
- [&P](int i){return P(i)==0;})-IP.data());
- </code></pre></td>
- <td>Where I is a vector of increasing indices from 0 to n, and P is a vector. <i>Requires C++11 and <code>#include <algorithm></code></i></td>
- </tr>
- <tr class=d0>
- <td><pre><code>B = A(R<2,C>1);</code></pre>
- <td><pre><code>B = igl::slice_mask(A,R.array()<2,C.array()>1);</code></pre>
- <td></td>
- </tr>
- <tr class=d1>
- <td><pre><code>a = any(A(:))</code></pre></td>
- <td><pre><code><del>bool a = any_of(A.data(),A.data()+A.size(),[](bool a){ return a;});</del>
- bool a = A.array().any();
- </code></pre></td>
- <td>Casts <code>Matrix::Scalar<code> to <code>bool</code>.</td>
- </tr>
- <tr class=d0>
- <td><pre><code>B = mod(A,2)</code></pre></td>
- <td><pre><code>igl::mod(A,2,B)</code></pre></td>
- <td></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>
- <br>
- <a href="./tutorial.html">IGL Lib Tutorial</a>
- </body>
- </html>
|