Here is the way. Not sure if it is considered vectorized.
Let your data matrix be denoted as x . Then
[ii, jj] = find([true(1,size(x,2)); ~x; true(1,size(x,2))]); result = accumarray(jj, ii, [], @(x){nonzeros(diff(x)-1)});
creates an array of cells where each cell corresponds to a column. In your example
result{1} = 2 11 3 result{2} = 13 3 result{3} = 6 11
How it works
The idea is to find the row and column indexes of zeros in x (i.e. true values in ~x ), and then use the column indices as grouping variables (first argument is accumarray ).
In each group, we use the anonymous function @(x){nonzeros(diff(x)-1)} to calculate the differences in the positions of the rows of zeros. We can apply diff directly because the column indices from find already sorted, thanks to the Matlab column major template. Subtract 1 because the zeros at x not considered part of the uptime; delete the idle time equal to 0 ( nonzeros ) and pack the resulting vector in the cell ( {...} ).
A string of true values is added and appended to ~x to make sure that we find the start and end periods of uptime.
source share