I have a vector with positive/negative signals and zeros (no signals):
X=[0,0,1,1,1,1,0,0,-1,-1,0,0,0,1,1,1,0,0,-1,0];
I want to create a new vector that is a cumulative summation of X
, with the condition that the cumulative sum may never exceed 3/-3 so that the output becomes:
Y=[0,0,1,2,3,3,3,3,2,1,1,1,1,2,3,3,3,3,2,2];
I can solve this through looping or arrayfun
but my matrices are large in size and multidimensional and neither of these solutions scale very well.
Can my problem be s开发者_如何学编程olved through a vectorized solution? Thanks.
Oli Charlesworth gave the answer to this in a comment. I am copying his comment as an answer so that this questions stops being shown among the unanswered questions.
Fundamentally, there is no vectorized solution, because each result element is dependent on the previous one. – Oli Charlesworth Sep 13 '11 at 17:01
精彩评论