title: Linear indexing of 3D matrix: best way to expand the first 2 dims to a vector (slow sprintf on scalars)
body: <p>I have a 3D vector <code>s1</code>(nmax,mmax,ntimeSTEPS). I want to take at each time step <code>j</code> (i.e. each value of the third dimension) all the elements of the first two dimensions and obtain a vector to give to <code>sprintf</code>. However, <code>sprintf</code> is PAINFULLY SLOW if inside a cycle! I checked the manual and it looks like there is no way to do that directly with linear indexing. Or am I missing something? I can only think of using <code>reshape</code>, but something like <code>s1(:,j)</code> would be the top, but that's not how MATLAB works. I did:</p>
  
  <pre><code>        nmax = 800;
          mmax =400;
          nmax_x_mmax = nmax*mmax;
          ntimeSTEPS = 1;
          charINPUT = cell(nmax_x_mmax,1);
          s1 = ones(nmax,mmax,ntimeSTEPS)*1234;
          tic
          for j=1:ntimeSTEPS
              %... other stuff
              input=reshape(s1(:,:,j),nmax_x_mmax,1);
              for kk=1:length(input)
                  charINPUT{kk} = sprintf('%6.3f',input(kk));
              end
              %... other stuff  (collecting movie frames etc)
          end
          toc
  </code></pre>
  
  <p>This on a single time steps takes 5.09 SECONDS on my i7 2.2 GHz! I am trying to do an animation and this is crazily slow. If I increase the size of the array its basically stuck.
  Any suggestion for doing this with linear indexes?</p>