matlab-to-eigen.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <html> <head>
  2. <title>MATLAB to Eigen</title>
  3. <link href="./style.css" rel="stylesheet" type="text/css">
  4. </head>
  5. <body>
  6. <table>
  7. <tr class="header">
  8. <th>MATLAB</th>
  9. <th>Eigen with libigl</th>
  10. <th>Notes</th>
  11. </tr>
  12. <tr class=d0>
  13. <td><pre><code>[Y,IX] = sort(X,dim,mode)</code></pre></td>
  14. <td><pre><code>igl::sort(X,dim,mode,Y,IX)</code></pre></td>
  15. <td>MATLAB version allows Y to be a multidimensional matrix, but the
  16. Eigen version is only for 1D or 2D matrices.</td>
  17. </tr>
  18. <tr class=d1>
  19. <td><pre><code>B(i:(i+w),j:(j+h)) = A(x:(x+w),y:(y+h))</code></pre></td>
  20. <td><pre><code>B.block(i,j,w,h) = A.block(i,j,w,h)</code></pre></td>
  21. <td>MATLAB version would allow w and h to be non-positive since the
  22. colon operator evaluates to a list of indices, but the Eigen version
  23. needs non-negative width and height values.</td>
  24. </tr>
  25. <tr class=d0>
  26. <td><pre><code>max(A(:))</code></pre></td>
  27. <td><pre><code>A.maxCoeff()</code></pre></td>
  28. <td>Find the maximum coefficient over all entries of the matrix.</td>
  29. </tr>
  30. <tr class=d1>
  31. <td><pre><code>min(A(:))</code></pre></td>
  32. <td><pre><code>A.minCoeff()</code></pre></td>
  33. <td>Find the minimum coefficient over all entries of the matrix.</td>
  34. </tr>
  35. <tr class=d0>
  36. <td><pre><code>eye(w,h)</code></pre></td>
  37. <td><pre><code>MatrixXd::Identity(w,h), MatrixXf::Identity(w,h), etc.</code></pre></td>
  38. <td></td>
  39. </tr>
  40. <tr class=d1>
  41. <td><pre><code>A(i:(i+w),j:(j+h)) = eye(w,h)</code></pre></td>
  42. <td><pre><code>A.block(i,j,w,h).setIdentity()</code></pre></td>
  43. <td></td>
  44. </tr>
  45. <tr class=d0>
  46. <td><pre><code>[I,J,V] = find(X)</code></pre></td>
  47. <td><pre><code>igl::find(X,I,J,V)</code></pre></td>
  48. <td>Matlab supports finding subscripts (I and J) as well as indices
  49. (just I), but so far igl::find only supports subscripts. Also,
  50. igl::find requires X to be sparse.</td>
  51. </tr>
  52. <tr class=d1>
  53. <td><pre><code>X(:,j) = X(:,j) + x</code></pre></td>
  54. <td><pre><code>X.col(j).array() += x</code></pre></td>
  55. <td></td>
  56. </tr>
  57. <tr class=d0>
  58. <td><pre><code>Acol_sum = sum(A,1)<br>Arow_sum = sum(A,2)<br>Adim_sum = sum(Asparse,dim)</code></pre></td>
  59. <td><pre><code>Acol_sum = A.colwise().sum()<br>Arow_sum = A.rowwise().sum()<br>igl::sum(Asparse,dim,Adim_sum)</code></pre></td>
  60. <td>Currently the igl version only supports sparse matrix input (and
  61. dim must be 1 or 2)</td>
  62. </tr>
  63. <tr class=d1>
  64. <td><pre><code>D = diag(M)</code></pre></td>
  65. <td><pre><code>igl::diag(M,D)</code></pre></td>
  66. <td>Extract the main diagonal of a matrix. Currently igl version
  67. supports sparse only.</td>
  68. </tr>
  69. <tr class=d0>
  70. <td><pre><code>M = diag(D)</code></pre></td>
  71. <td><pre><code>igl::diag(D,M)</code></pre></td>
  72. <td>Construct new square matrix M with entries of vector D along the
  73. diagonal. Currently igl version supports sparse only.</td>
  74. </tr>
  75. <tr class=d1>
  76. <td><pre><code>[Y,I] = max(X,[],dim)</code></pre></td>
  77. <td id=mat_max><pre><code>igl::mat_max(X,dim,Y,I)</code></pre></td>
  78. <td>Matlab has a bizarre convention of passing [] as the second
  79. argument to mean take the max/min along dimension dim.</td>
  80. </tr>
  81. <tr class=d0>
  82. <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>
  83. <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>
  84. <td>Matlab allows you to obtain the indices of extrema see <a href=#mat_max>mat_max</a></td>
  85. </tr>
  86. <tr class=d1>
  87. <td><pre><code>C = A.*B</code></pre></td>
  88. <td><pre><code>C = (A.array() * B.array()).matrix()</code></pre></td>
  89. <td></td>
  90. </tr>
  91. <tr class=d0>
  92. <td><pre><code>C = A.^b</code></pre></td>
  93. <td><pre><code>C = A.array().pow(b).matrix()</code></pre></td>
  94. <td></td>
  95. </tr>
  96. <tr class=d1>
  97. <td><pre><code>A(B == 0) = C(B==0)</code></pre></td>
  98. <td><pre><code>A = (B.array() == 0).select(C,A)</code></pre></td>
  99. <td></td>
  100. </tr>
  101. <tr class=gotcha1>
  102. <td><pre><code>C = A + B'</code></pre></td>
  103. <td><pre><code>SparseMatrixType BT = B.transpose()<br>SparseMatrixType C = A+BT;</code></pre></td>
  104. <td>Do <b>not</b> attempt to combine .transpose() in expression like
  105. this: <pre><code>C = A + B.transpose()</code></pre></td>
  106. </tr>
  107. <tr class=gotcha2>
  108. <td><pre><code>[L,p] = chol(A)</code></pre></td>
  109. <td><pre><code>SparseLLT&lt;SparseMatrixType&gt; A_LLT(A.template triangularView&lt;Lower&gt;())<br>SparseMatrixType L = A_LLT.matrixL();bool p = (L*0).eval().nonZeros()==0;</code></pre></td>
  110. <td>Do <b>not</b> attempt to use A in constructor of A_LLT like
  111. this: <pre><code>SparseLLT&lt;SparseMatrixType&gt; A_LLT(A)</code></pre><br>
  112. Do <b>not</b> attempt to use A_LLT.succeeded() to determine if Cholesky
  113. factorization succeeded, like this:
  114. <pre><code>bool p = A_LLT.succeeded()</code></pre>
  115. </td>
  116. </tr>
  117. <tr class=d0>
  118. <td><pre><code>X = U\(L\b)</code></pre></td>
  119. <td><pre><code>X = b;<br>L.template triangularView&lt;Lower&gt;().solveInPlace(X);<br>U.template triangularView&lt;Upper&gt;().solveInPlace(X);</code></pre></td>
  120. <td>Expects that L and U are lower and upper triangular matrices
  121. respectively</td>
  122. </tr>
  123. <tr class=d1>
  124. <td><pre><code>B = repmat(A,i,j)</code></pre></td>
  125. <td><pre><code>igl::repmat(A,i,j,B);
  126. B = A.replicate(i,j);</code></pre></td>
  127. <td>igl::repmat is also implemented for Sparse Matrices.
  128. </td>
  129. </tr>
  130. <tr class=d0>
  131. <td><pre><code>I = low:step:hi</code></pre></td>
  132. <td><pre><code>igl::colon(low,step,hi,I);
  133. // or
  134. const int size = ((hi-low)/step)+1;
  135. I = VectorXiLinSpaced(size,low,low+step*(size-1));</code></pre></td>
  136. <td>IGL version should be templated enough to handle same situations as
  137. matlab's colon. The matlab keyword <b>end</b> does not correspond in
  138. the C++ version. You'll have to use M.size(),M.rows() or whatever.
  139. </td>
  140. </tr>
  141. <tr class=d1>
  142. <td><pre><code>O = ones(m,n)</code></pre></td>
  143. <td><pre><code>Matrix* O = Matrix*::Ones(m,n)</code></pre></td>
  144. <td></td>
  145. </tr>
  146. <tr class=d0>
  147. <td><pre><code>O = zeros(m,n)</code></pre></td>
  148. <td><pre><code>Matrix* O = Matrix*::Zero(m,n)</code></pre></td>
  149. <td></td>
  150. </tr>
  151. <tr class=d1>
  152. <td><pre><code>B = A(I,J)<br>B = A(I,:)</code></pre></td>
  153. <td><pre><code>igl::slice(A,I,J,B)<br>B = igl::slice(A,I,igl::colon(0,A.cols()-1))</code></pre></td>
  154. <td>This is only for the case when I and J are lists of indices and
  155. not vectors of logicals.</td>
  156. </tr>
  157. <tr class=d0>
  158. <td><pre><code>B(I,J) = A<br>B(I,:) = A</code></pre></td>
  159. <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>
  160. <td>This is only for the case when I and J are lists of indices and
  161. not vectors of logicals.</td>
  162. </tr>
  163. <tr class=d1>
  164. <td><pre><code>M = mode(X,dim)</code></pre></td>
  165. <td><pre><code>igl::mode(X,dim,M)</code></pre></td>
  166. <td></td>
  167. </tr>
  168. <tr class=d0>
  169. <td><pre><code>B = arrayfun(FUN, A)</code></pre></td>
  170. <td><pre><code>B = A.unaryExpr(ptr_fun(FUN))</code></pre></td>
  171. <td>If FUN is templated, the templates must be fully resolved.</td>
  172. </tr>
  173. <tr class=d1>
  174. <td><pre><code>B = fliplr(A)<br>B = flipud(A)</code></pre></td>
  175. <td><pre><code>B = A.rowwise().reverse().eval()<br>B =
  176. A.colwise().reverse().eval()</code></pre></td>
  177. <td>The <code>.eval()</code> is not necessary if A != B</td>
  178. </tr>
  179. <tr class=d0>
  180. <td><pre><code>B = IM(A)
  181. A = IM(A);
  182. </code></pre></td>
  183. <td><pre><code>B = A.unaryExpr(bind1st(mem_fun(
  184. static_cast&lt;VectorXi::Scalar&amp;(VectorXi::*)(VectorXi::Index)&gt;
  185. (&amp;VectorXi::operator())), &amp;IM)).eval();
  186. // or
  187. for_each(A.data(),A.data()+A.size(),[&amp;IM](int &amp; a){a=IM(a);});
  188. </code></pre></td>
  189. <td>Where IM is an "index map" column vector and A is an arbitrary
  190. matrix. The <code>.eval()</code> is not necessary if A != B, but then
  191. the second option should be used.</td>
  192. </tr>
  193. <tr class=d1>
  194. <td><pre><code>A = sparse(I,J,V)</code></pre></td>
  195. <td><pre><code>// build std::vector&ltEigen::Triplet&gt; IJV
  196. A.setFromTriplets(IJV);
  197. </code></pre></td>
  198. <td>IJV and A should not be empty! (this might be fixed)</td>
  199. </tr>
  200. <tr class=d0>
  201. <td><pre><code>A = min(A,c);</code></pre></td>
  202. <td><pre><code>C.array() = A.array().min(c);
  203. </code></pre></td>
  204. <td>Coefficient-wise minimum of matrix and scalar (or matching size matrix)</td>
  205. </tr>
  206. <tr class=d1>
  207. <td><pre><code>I=1:10;
  208. ...
  209. IP = I(P==0);</code></pre></td>
  210. <td><pre><code>I = VectorXi::LinSpaced(10,0,9);
  211. ...
  212. VectorXi IP = I;
  213. IP.conservativeResize(stable_partition(
  214. IP.data(),
  215. IP.data()+IP.size(),
  216. [&amp;P](int i){return P(i)==0;})-IP.data());
  217. </code></pre></td>
  218. <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 &lt;algorithm&gt;</code></i></td>
  219. </tr>
  220. <tr class=d0>
  221. <td><pre><code>B = A(R&lt;2,C&gt;1);</code></pre>
  222. <td><pre><code>B = igl::slice_mask(A,R.array()&lt;2,C.array()&gt;1);</code></pre>
  223. <td></td>
  224. </tr>
  225. <tr class=d1>
  226. <td><pre><code>a = any(A(:))</code></pre></td>
  227. <td><pre><code><del>bool a = any_of(A.data(),A.data()+A.size(),[](bool a){ return a;});</del>
  228. bool a = A.array().any();
  229. </code></pre></td>
  230. <td>Casts <code>Matrix::Scalar<code> to <code>bool</code>.</td>
  231. </tr>
  232. <tr class=d0>
  233. <td><pre><code>B = mod(A,2)</code></pre></td>
  234. <td><pre><code>igl::mod(A,2,B)</code></pre></td>
  235. <td></td>
  236. </tr>
  237. <!-- Insert rows for each command pair -->
  238. <!-- Leave this here for copy and pasting
  239. <tr class=d0>
  240. <td><pre><code>Matlab code</code></pre></td>
  241. <td><pre><code>Eigen code</code></pre></td>
  242. <td>Notes</td>
  243. </tr>
  244. -->
  245. </table>
  246. <a href="http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt">Eigen's
  247. "ASCII Quick Reference" with MATLAB translations</a>
  248. <br>
  249. <a href="./tutorial.html">IGL Lib Tutorial</a>
  250. </body>
  251. </html>