MATLAB: How to replace elements in a vector if a certain condition is true

replacing elements

Hi, I need some help with the following problem: I have a vector that contains negative numbers, zeros & ones. For example, one sequence could look like this
-5 0 0 0 0 1 0 1 0 0 0 -3 0 0 0 1 0 0 1 0 0 1 0 -1 0 0 0 1 0 0 1...
What I want to do is find out if there are only two '1's between one negative number and the next negative number (while the '0's are not important). If that is not the case, for instance -3 above is followed by 3 '1's before the next negative number occurs, I would like to replace this negative number (-3) & the following '1's with 0. So my result would be:
-5 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 1...
I hope that makes sense & that someone can help me!

Best Answer

other variant:
A = [-5 0 0 0 0 1 0 1 0 0 0 -3 0 0 0 1 0 0 1 0 0 1 0 -1 0 0 0 1 0 0 1]
out = A(:);
As = sign(A(:));
t = find(As ~= 0);
ii = cumsum(As(t)==-1);
jj = accumarray(ii,1) > 3;
out(t(jj(ii))) = 0