title: How to convert an {Mx1} cell array of {1xN cell} arrays into a {1xN} cell array of {Mx1 cell} arrays?
body: <p>Suppose that <code>C</code> is a cell array with shape <em>M</em>&nbsp;&times;&nbsp;1 (i.e., <code>size(C)</code> returns [<em>M</em>&nbsp;1]), and that each element of <code>C</code> is in turn a cell array with shape 1&nbsp;&times;&nbsp;<em>N</em>.</p>
  
  <p>I often want to convert such a cell array to a new cell array <code>D</code> having shape 1&nbsp;&times;&nbsp;<em>N</em>, with elements being cell arrays with shape <em>M</em>&nbsp;&times;&nbsp;1, and such that <code>C{i}{j}</code> equals <code>D{j}{i}</code> for all 0&nbsp;&lt;&nbsp;<em>i</em>&nbsp;&leq;&nbsp;<em>M</em>, and 0&nbsp;&lt;&nbsp;<em>j</em>&nbsp;&leq;&nbsp;<em>N</em>.</p>
  
  <p>I use the following monstrosity for this</p>
  
  <pre><code>D = arrayfun(@(j) arrayfun(@(i) C{i}{j}, (1:M)', 'un', 0), 1:N, 'un', 0);
  </code></pre>
  
  <p>but the need for this operation arises often enough (after all, it's sort of a "cell array transpose") that I thought I'd ask:</p>
  
  <blockquote>
    <p><strong>is there a more standard way to do this operation?</strong></p>
  </blockquote>
  
  <p>Note that the desired <code>D</code> is different from</p>
  
  <pre><code>E = cat(2, C{:});
  </code></pre>
  
  <p>or, equivalently,</p>
  
  <pre><code>E = cat(1, D{:});
  </code></pre>
  
  <p>The <code>E</code> above is a two-dimensional (<em>M</em>&nbsp;&times;&nbsp;<em>N</em>) cell array, whereas both <code>C</code> and <code>D</code> are one-dimensional cell arrays of one-dimensional cell arrays. Of course, conversion of <code>E</code> back to either <code>C</code> or <code>D</code> is also another often needed operation (this sort of thing is never-ending with MATLAB), but I'll leave it for another post.</p>
  
  <hr>
  
  <p><sub>The motivation behind this question goes far beyond the problem described above. It turns out that a <em>huge</em> fraction of my MATLAB code, and an even larger fraction of my MATLAB programming <em>time and effort</em>, are devoted to this <em>essentially unproductive</em> chore of converting data from one format to another. Of course, format conversion is unavoidable when doing any kind of computational work, but with MATLAB I find myself doing it a lot more, or at least having to work <em>a lot harder</em> at it, than when I work in other systems (e.g., <em>Mathematica</em> or Python/NumPy). My hope is that by building up my repertoire of MATLAB "format conversion tricks" I will be able to bring down to a more reasonable level the fraction of my MATLAB programming time that I have to devote to format conversion.
  </sub></p>
  
  <hr>
  
  <p>P.S. The following code constructs a <code>C</code> like the one described above, for <em>M</em>&nbsp;=&nbsp;5 and <em>N</em>&nbsp;=&nbsp;2.</p>
  
  <pre><code>uc = ['A':'Z'];
  randstr = @() uc(randi(26, [1 4]));
  M = 5;
  rng(0);  % keep example reproducible
  C = arrayfun(@(i) {randstr() i}, 1:M, 'un', 0)';
  
  % C = 
  %     {1x2 cell}
  %     {1x2 cell}
  %     {1x2 cell}
  %     {1x2 cell}
  %     {1x2 cell}
  % &gt;&gt; cat(1, C{:});
  % ans = 
  %     'VXDX'    [1]
  %     'QCHO'    [2]
  %     'YZEZ'    [3]
  %     'YMUD'    [4]
  %     'KXUY'    [5]
  %     
  
  N = 2;
  D = arrayfun(@(j) arrayfun(@(i) C{i}{j}, (1:M)', 'un', 0), 1:N, 'un', 0);
  
  % D = 
  %     {5x1 cell}    {5x1 cell}
  </code></pre>