MATLAB: How to count the number of consecutive negative values in a vector

countingvectorization

Hello! What is the most time-saving method (using the vectorization style or others but not for loops) to count the maximum number of consecutive negative values in a vector?
The problem is that there are thousands of vectors of the same length and if one contains more than 3 consecutive negative values then it will be removed from the data set. For example, vector v1 will be deleted due to [-1 -1 -3] chain, but v2 will remain:
v1 = [11 2 3 -1 -2 1 -1 -1 -3 1 3 1]
v2 = [3 -1 4 1 33 44 -11 -3 4 11 3 -1]
Can it be get done by using array modifying functions only (like 'repmat', 'diff', 'cumsum' etc. not consuming ones like 'find')?
Many Thanks!

Best Answer

This might work using cumsum to find all the negatives in a group and hist to count the consecutive negatives
v1 = [11 2 3 -1 -2 1 -1 -1 -3 -1 1 3 1];
v2 = [3 -1 4 1 33 44 -11 -3 4 11 3 -1];
x = [0 cumsum(v1>=0)]; %makes all the values positive increment the sum, so all negative numbers will keep the next number the same
numNeg=max(hist(x)-1); % find number of consecutive negative by looking counting all the similar numbers and subtracting 1 for the initial increment