title: Matlab conditional sum of signals in matrix
body: <p>I have a vector with positive/negative signals and zeros (no signals):</p>
  
  <pre><code>X=[0,0,1,1,1,1,0,0,-1,-1,0,0,0,1,1,1,0,0,-1,0];
  </code></pre>
  
  <p>I want to create a new vector that is a cumulative summation of <code>X</code>, with the condition that the cumulative sum may never exceed 3/-3 so that the output becomes:</p>
  
  <pre><code>Y=[0,0,1,2,3,3,3,3,2,1,1,1,1,2,3,3,3,3,2,2];
  </code></pre>
  
  <p>I can solve this through looping or <code>arrayfun</code> but my matrices are large in size and multidimensional and neither of these solutions scale very well.</p>
  
  <p>Can my problem be solved through a vectorized solution? Thanks.</p>