Let vec be row (1-by- n ) zeros and ones, then you can use the following code
rl = ( find( vec ~= [vec(2:end), vec(end)+1] ) ); data = vec( rl ); rl(2:end) = rl(2:end) - rl(1:end-1);
rl will give you the number of consecutive zeros and ones, and data will tell you about each block if it is zero or one.
This issue is closely related to the path length .
Demo:
vec = [1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1]; rl = ( find( vec ~= [vec(2:end), vec(end)+1] ) ); data = vec( rl ), rl(2:end) = rl(2:end) - rl(1:end-1), data = 1 0 1 0 1 rl = 4 3 2 14 8
source share