-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindStartStop.m
More file actions
34 lines (26 loc) · 980 Bytes
/
findStartStop.m
File metadata and controls
34 lines (26 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
% OUTPUT: start_inds, stop_inds
function [varargout] = findStartStop(binary_vector)
% Take differential of binary_vector
diff_vector = diff(binary_vector);
% Adjust diffential to match length & check if 1st frame is 1/0
if binary_vector(1) == 1
diff_vector = [1 diff_vector];
elseif binary_vector(1) == 0
diff_vector = [0 diff_vector];
end
% Adjust diffential if last frame is positive
if binary_vector(end) == 1
diff_vector = [diff_vector -1];
end
% Extract start and stop inds from diff_vector (start frame = 1; stop (+ 1) frame = -1)
start_inds = find(diff_vector' == 1);
stop_inds = find(diff_vector' == -1) - 1;
nOutputs = nargout;
varargout = cell(1, nOutputs);
if nOutputs == 1
varargout{1} = [start_inds, stop_inds];
elseif nOutputs == 2
varargout{1} = start_inds;
varargout{2} = stop_inds;
end
end