matlab-to-eigen.html 9.0 KB

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