title: matlab: addition of symbolic matrices along an unused dimension
body: <p>How can symbolic matrices collected in a cell array be summed along an unused dimension? Assume, for reasons beyond the scope of this post, that the matrices must be the elements of a cell array, rather than planes of a higher-dimensional matrix. What follows is a brief demonstration of the problem.</p>
  
  <p>Suppose we have a cell array of 2D <code>sym</code> vectors prepared as follows:</p>
  
  <pre><code>a = vpa(ones(2,2));
  c = {a; 2*a; 3*a};
  </code></pre>
  
  <p>Despite the fact that it is possible to make multidimensional symbolic matrices with newer versions of matlab (e.g., <code>sym('c', [2 2 2])</code>), addition of 2D <code>sym</code> matrices along a third dimension fails</p>
  
  <pre><code>sum(cat(3, c{:}), 3)
      Error using symengine
      Arguments must be 2-dimensional.
  </code></pre>
  
  <p>With numeric inputs, the operation is successful:</p>
  
  <pre><code>a = ones(2,2);
  c = {a; 2*a; 3*a};
  sum(cat(3,c{:}), 3)
      6    6
      6    6
  </code></pre>
  
  <p>Edit:</p>
  
  <p>In fact, the problem of multidimensional addition is more general than I had thought: <code>sum</code> cannot be applied to any <code>sym</code> matrix of dimensionality greater than 2.</p>
  
  <pre><code>a=vpa(ones(2,2));
  sum(a,1)
      [ 2.0, 2.0]
  sum(a,2)
       2.0
       2.0
  a = vpa(ones(2,2,2));
  sum(a,1) % fails
  sum(a,2) % fails
  sum(a,3) % fails
  </code></pre>
  
  <p>Manual addition of planes along a third dimension works, so iterative addition is an option, though <code>sum</code> would be preferable:</p>
  
  <pre><code>a=vpa(ones(2,2,2));
  plus(a(:,:,1), a(:,:,2))
      [ 2.0, 2.0]
      [ 2.0, 2.0]
  </code></pre>
  
  <p>Edit #2:</p>
  
  <p>According to <a href="https://www.mathworks.com/help/symbolic/symbolic-summation.html" rel="nofollow noreferrer">this page</a>, the problem is apparently unavoidable since support for dimensionality >2 is explicitly not offered.</p>
  
  <blockquote>
    <p>Unlike the MATLAB® sum, the symbolic sum function does not work on multidimensional arrays. For details, follow the MATLAB sum page.</p>
  </blockquote>
  
  <p>Nevertheless, suggestions on alternatives are still welcome.</p>