title: How to vectorize this code involving matrix pages in MATLAB?
body: <p>Is it possible to vectorize, and possibly run on a GPU, the following code</p>
  
  <pre><code>x = linspace(0,100,1000);
  h = zeros(size(x));
  
  for i = 1 : length(x)
      exprho = expm(-x(i)*rho);
      h(i) = trace(drho*exprho*drho*exprho);
  end
  
  out = 2 * trapz(x,h);
  </code></pre>
  
  <p>where <code>rho</code> and <code>drho</code> are two complex Hermitian square matrices of the same size. <code>rho</code> is in fact a quantum density matrix and <code>drho</code> is its derivative with respect to a parameter.
  The size can range from <code>10 x 10</code> to <code>300 x 300</code> approximately but I would also like to reach bigger sizes. </p>
  
  <p>Here are two sample matrices:</p>
  
  <pre><code>rho =
  
  0.4046    0.3849    0.2589    0.1422    0.0676    0.0288    0.0112    0.0040    0.0014    0.0004    0.0001
  0.3849    0.3661    0.2462    0.1352    0.0643    0.0274    0.0106    0.0038    0.0013    0.0004    0.0001
  0.2589    0.2462    0.1656    0.0910    0.0433    0.0184    0.0071    0.0026    0.0009    0.0003    0.0001
  0.1422    0.1352    0.0910    0.0500    0.0238    0.0101    0.0039    0.0014    0.0005    0.0002    0.0000
  0.0676    0.0643    0.0433    0.0238    0.0113    0.0048    0.0019    0.0007    0.0002    0.0001    0.0000
  0.0288    0.0274    0.0184    0.0101    0.0048    0.0020    0.0008    0.0003    0.0001    0.0000    0.0000
  0.0112    0.0106    0.0071    0.0039    0.0019    0.0008    0.0003    0.0001    0.0000    0.0000    0.0000
  0.0040    0.0038    0.0026    0.0014    0.0007    0.0003    0.0001    0.0000    0.0000    0.0000    0.0000
  0.0014    0.0013    0.0009    0.0005    0.0002    0.0001    0.0000    0.0000    0.0000    0.0000    0.0000
  0.0004    0.0004    0.0003    0.0002    0.0001    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
  0.0001    0.0001    0.0001    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
  
  
  drho =
  
   0.0366    0.0156   -0.0025   -0.0085   -0.0074   -0.0046   -0.0023   -0.0010   -0.0004   -0.0002   -0.0001
   0.0156   -0.0035   -0.0147   -0.0148   -0.0103   -0.0057   -0.0028   -0.0012   -0.0005   -0.0002   -0.0001
  -0.0025   -0.0147   -0.0181   -0.0145   -0.0091   -0.0048   -0.0022   -0.0009   -0.0004   -0.0001   -0.0000
  -0.0085   -0.0148   -0.0145   -0.0105   -0.0062   -0.0031   -0.0014   -0.0006   -0.0002   -0.0001   -0.0000
  -0.0074   -0.0103   -0.0091   -0.0062   -0.0035   -0.0017   -0.0008   -0.0003   -0.0001   -0.0000   -0.0000
  -0.0046   -0.0057   -0.0048   -0.0031   -0.0017   -0.0008   -0.0004   -0.0001   -0.0001   -0.0000   -0.0000
  -0.0023   -0.0028   -0.0022   -0.0014   -0.0008   -0.0004   -0.0002   -0.0001   -0.0000   -0.0000   -0.0000
  -0.0010   -0.0012   -0.0009   -0.0006   -0.0003   -0.0001   -0.0001   -0.0000   -0.0000   -0.0000   -0.0000
  -0.0004   -0.0005   -0.0004   -0.0002   -0.0001   -0.0001   -0.0000   -0.0000   -0.0000   -0.0000   -0.0000
  -0.0002   -0.0002   -0.0001   -0.0001   -0.0000   -0.0000   -0.0000   -0.0000   -0.0000   -0.0000   -0.0000
  -0.0001   -0.0001   -0.0000   -0.0000   -0.0000   -0.0000   -0.0000   -0.0000   -0.0000   -0.0000   -0.0000
  </code></pre>