MATLAB: Finding Sequences of 1’s values

burstcountingdurationextremefindingsequenceseries

I have a matrix of values, for example: [0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 …]
In practice this is a vector (1 row by somewhere around 80000 columns). I need to output a vector with the duration of these 'bursts' of 1's. For example, the output for the above would be: [3 2 5 …]
Background: I have a time series where all the 1's are values above a threshold and all the 0's are values below a threshold. So by doing this I'm trying to investigate the duration of these extreme events.
Can anyone tell me how I can do this?

Best Answer

If you have the Image Processing Toolbox, just call bwconncomp() or bwlabel(), then call regionprops() asking for 'Area' and then extract the areas from the structure. Three lines.
labeledMatrix = bwlabel(data);
measurements = regionprops(labeledMatrix, 'Area');
allAreas = [measurements.Area];