Skip to content

Instantly share code, notes, and snippets.

@ferryzhou
Created March 31, 2012 21:30
Show Gist options
  • Save ferryzhou/2268713 to your computer and use it in GitHub Desktop.
Save ferryzhou/2268713 to your computer and use it in GitHub Desktop.
Get segments from a binary vector
function segs = get_segments(o)
segs = [];
if o(1), st = 1; state = 1;
else state = 2; end
for i = 2 : length(o)
if o(i)
if state == 2
st = i; state = 1;
end
else
if state == 1
ed = i - 1; state = 2;
segs = [segs; [st ed]];
end
end
end
if state == 1
segs = [segs; [st i]];
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment