matlab-to-eigen.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <html>
  2. <head>
  3. <title>MATLAB to Eigen</title>
  4. <style type="text/css">
  5. table
  6. {
  7. border-spacing: 0px;
  8. }
  9. tr.header th
  10. {
  11. border-bottom: 1px solid;
  12. background-color: #ffb;
  13. padding-left: 10px;
  14. padding-right: 20px;
  15. }
  16. tr.d0 td
  17. {
  18. background-color: #EEE;
  19. color: black;
  20. padding-left: 10px;
  21. padding-right: 20px;
  22. min-width: 200px;
  23. }
  24. tr.d1 td
  25. {
  26. background-color: #bcf;
  27. color: black;
  28. padding-left: 10px;
  29. padding-right: 20px;
  30. min-width: 200px;
  31. }
  32. tr.gotcha1 td
  33. {
  34. background-color: #fcb;
  35. color: black;
  36. padding-left: 10px;
  37. padding-right: 20px;
  38. min-width: 200px;
  39. }
  40. tr.gotcha2 td
  41. {
  42. background-color: #fed;
  43. color: black;
  44. padding-left: 10px;
  45. padding-right: 20px;
  46. min-width: 200px;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <table>
  52. <tr class="header">
  53. <th>MATLAB</th>
  54. <th>Eigen</th>
  55. <th>Notes</th>
  56. </tr>
  57. <tr class=d0>
  58. <td><pre><code>[Y,IX] = sort(Y,dim,mode)</code></pre></td>
  59. <td><pre><code>igl::sort(X,dim,mode,Y,IX)</code></pre></td>
  60. <td>MATLAB version allows Y to be a multidimensional matrix, but the
  61. Eigen version is only for 1D or 2D matrices.</td>
  62. </tr>
  63. <tr class=d1>
  64. <td><pre><code>B(i:(i+w),j:(j+h)) = A(x:(x+w),y:(y+h))</code></pre></td>
  65. <td><pre><code>B.block(i,j,w,h) = A.block(i,j,w,h)</code></pre></td>
  66. <td>MATLAB version would allow w and h to be non-positive since the
  67. colon operator evaluates to a list of indices, but the Eigen version
  68. needs non-negative width and height values.</td>
  69. </tr>
  70. <tr class=d0>
  71. <td><pre><code>max(A(:))</code></pre></td>
  72. <td><pre><code>A.maxCoeff()</code></pre></td>
  73. <td>Find the maximum coefficient over all entries of the matrix.</td>
  74. </tr>
  75. <tr class=d1>
  76. <td><pre><code>min(A(:))</code></pre></td>
  77. <td><pre><code>A.minCoeff()</code></pre></td>
  78. <td>Find the minimum coefficient over all entries of the matrix.</td>
  79. </tr>
  80. <tr class=d0>
  81. <td><pre><code>eye(w,h)</code></pre></td>
  82. <td><pre><code>MatrixXd::Identity(w,h), MatrixXf::Identity(w,h), etc.</code></pre></td>
  83. <td></td>
  84. </tr>
  85. <tr class=d1>
  86. <td><pre><code>A(i:(i+w),j:(j+h)) = eye(w,h)</code></pre></td>
  87. <td><pre><code>A.setIdentity()</code></pre></td>
  88. <td></td>
  89. </tr>
  90. <tr class=d0>
  91. <td><pre><code>[I,J,V] = find(X)</code></pre></td>
  92. <td><pre><code>igl::find(X,I,J,V)</code></pre></td>
  93. <td>Matlab supports finding subscripts (I and J) as well as indices
  94. (just I), but so far igl::find only supports subscripts. Also,
  95. igl::find requires X to be sparse.</td>
  96. </tr>
  97. <tr class=d1>
  98. <td><pre><code>X(:,j) = X(:,j) + x</code></pre></td>
  99. <td><pre><code>X.col(j).array() += x</code></pre></td>
  100. <td></td>
  101. </tr>
  102. <tr class=d0>
  103. <td><pre><code>Acol_sum = sum(A,1)<br>Arow_sum = sum(A,2)<br>Adim_sum = sum(Asparse,dim)</code></pre></td>
  104. <td><pre><code>Acol_sum = A.colwise().sum()<br>Arow_sum = A.rowwise().sum()<br>igl::sum(Asparse,dim,Adim_sum)</code></pre></td>
  105. <td>Currently the igl version only supports sparse matrix input (and
  106. dim must be 1 or 2)</td>
  107. </tr>
  108. <tr class=d1>
  109. <td><pre><code>D = diag(M)</code></pre></td>
  110. <td><pre><code>igl::diag(M,D)</code></pre></td>
  111. <td>Extract the main diagonal of a matrix. Currently igl version
  112. supports sparse only.</td>
  113. </tr>
  114. <tr class=d0>
  115. <td><pre><code>M = diag(D)</code></pre></td>
  116. <td><pre><code>igl::diag(D,M)</code></pre></td>
  117. <td>Construct new square matrix M with entries of vector D along the
  118. diagonal. Currently igl version supports sparse only.</td>
  119. </tr>
  120. <tr class=d1>
  121. <td><pre><code>[Y,I] = max(X,[],dim)</code></pre></td>
  122. <td id=mat_max><pre><code>igl::mat_max(X,dim,Y,I)</code></pre></td>
  123. <td>Matlab has a bizarre convention of passing [] as the second
  124. argument to mean take the max/min along dimension dim.</td>
  125. </tr>
  126. <tr class=d0>
  127. <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>
  128. <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>
  129. <td>Matlab allows you to obtain the indices of extrema see <a href=#mat_max>mat_max</a></td>
  130. </tr>
  131. <tr class=d1>
  132. <td><pre><code>C = A.*B</code></pre></td>
  133. <td><pre><code>C = (A.array() * B.array()).matrix()</code></pre></td>
  134. <td></td>
  135. </tr>
  136. <tr class=d0>
  137. <td><pre><code>C = A.^b</code></pre></td>
  138. <td><pre><code>C = A.array().pow(b).matrix()</code></pre></td>
  139. <td></td>
  140. </tr>
  141. <tr class=d1>
  142. <td><pre><code>A(B == 0) = C</code></pre></td>
  143. <td><pre><code>A = (B == 0).select(C,A)</code></pre></td>
  144. <td></td>
  145. </tr>
  146. <tr class=gotcha1>
  147. <td><pre><code>C = A + B'</code></pre></td>
  148. <td><pre><code>SparseMatrixType BT = B.transpose()<br>SparseMatrixType C = A+BT;</code></pre></td>
  149. <td>Do <b>not</b> attempt to combine .transpose() in expression like
  150. this: <pre><code>C = A + B.transpose()</code></pre></td>
  151. </tr>
  152. <tr class=gotcha2>
  153. <td><pre><code>[L,p] = chol(A)</code></pre></td>
  154. <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>
  155. <td>Do <b>not</b> attempt to use A in constructor of A_LLT like
  156. this: <pre><code>SparseLLT&lt;SparseMatrixType&gt; A_LLT(A)</code></pre><br>
  157. Do <b>not</b> attempt to use A_LLT.succeeded() to determine if Cholesky
  158. factorization succeeded, like this:
  159. <pre><code>bool p = A_LLT.succeeded()</code></pre>
  160. </td>
  161. </tr>
  162. <tr class=d0>
  163. <td><pre><code>X = U\(L\b)</code></pre></td>
  164. <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>
  165. <td>Expects that L and U are lower and upper triangular matrices
  166. respectively</td>
  167. </tr>
  168. <tr class=d1>
  169. <td><pre><code>B = repmat(A,i,j)</code></pre></td>
  170. <td><pre><code>igl::repmat(A,i,j,B)</code></pre></td>
  171. <td>So far igl::repmat is only implemented for dense matrices.
  172. </td>
  173. </tr>
  174. <tr class=d0>
  175. <td><pre><code>I = low:step:hi</code></pre></td>
  176. <td><pre><code>igl::colon(low,step,hi,I)</code></pre></td>
  177. <td>IGL version should be templated enough to handle same situations as
  178. matlab's colon. The matlab keyword <b>end</b> does not correspond in
  179. the C++ version. You'll have to use M.size(),M.rows() or whatever.
  180. </td>
  181. </tr>
  182. <!-- Insert rows for each command pair -->
  183. <!-- Leave this here for copy and pasting
  184. <tr class=d0>
  185. <td><pre><code>Matlab code</code></pre></td>
  186. <td><pre><code>Eigen code</code></pre></td>
  187. <td>Notes</td>
  188. </tr>
  189. -->
  190. </table>
  191. <a href="http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt">Eigen's
  192. "ASCII Quick Reference" with MATLAB translations</a>
  193. </body>
  194. </html>