MATLAB: How to get the sum of numbers below a desired number in sets

consecutive number additionMATLAB

How to get the sum of the numbers in the array which are below -1 in sets.
For example
a=[1 -2 0 .5 -3 -4 5 7 -2 -3 -4];
so here the numbers which are consecutively less than -1 should be added only.
i.e. The answer should be
b=[-2 -7 -10]
PS: Actual data is very big.

Best Answer

These two lines of code will do it.
a = [1 -2 0 .5 -3 -4 5 7 -2 -3 -4];
props = regionprops(a < -1, a, 'Area', 'MeanIntensity');
output = [props.Area] .* [props.MeanIntensity]
How big are your arrays? This should be pretty speedy for up to tens or hundreds of millions of elements. If you have gigabyte sized arrays, it could take a few minutes. The code requires the Image Processing Toolbox.