title: How can I add a trailing singleton dimension to a matrix
body: <p>As the title says, how can I add a trailing singleton dimension to a matrix in Matlab?</p>
  
  <p><code>A=ones(3,3,1);</code> gives a 3x3 matrix, while <code>A=ones(1,3,3);</code> gives a 1x3x3 matrix</p>
  
  <p>Adding to the specific problem: </p>
  
  <p>I have an application where I have N MxM matrices and I need to stack them, the result becoming a MxMxN matrix. However, N can be 1, and if it is, I need the matrix to be MxMx1.</p>
  
  <p>Note: I am aware that this makes little sense for Matlab scripts, as <a href="http://uk.mathworks.com/matlabcentral/newsreader/view_thread/293216" rel="nofollow noreferrer">Loren suggested</a>, in Matlab there are infinite "singleton" dimensions after non singleton ones. However, this is otherwise in the mex environment, where <code>mxGetNumberOfDimensions</code> is used. </p>