Skip to content

Commit df427d3

Browse files
authored
Merge pull request #270 from NeurodataWithoutBorders/fix/findshapes-slow
Fix/findshapes slow
2 parents 1c4dadc + b0b99ab commit df427d3

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

+types/+untyped/+datastub/findShapes.m

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,29 @@
3939
start = 1;
4040
step = 1;
4141
count = 0;
42-
for i = 1:length(indices)
43-
tempStart = indices(i);
44-
if length(indices) - i <= count
45-
% number of elements cannot possibly be larger than what we have.
42+
for stepInd = 2:length(indices)
43+
tempStep = indices(stepInd) - indices(1);
44+
idealRange = indices(1):tempStep:indices(end);
45+
if length(idealRange) <= count
4646
break;
4747
end
48-
for j = 1:(length(indices)-i)
49-
tempStep = indices(i+j) - indices(i);
50-
for k = length(indices):-1:i
51-
tempStop = indices(k);
52-
idealRange = tempStart:tempStep:tempStop;
53-
rangeMatches = ismembc(idealRange, indices);
54-
numMatches = sum(rangeMatches);
55-
if numMatches <= count
56-
% number of intersected items is shorter than what we have.
57-
break;
58-
end
59-
if all(rangeMatches)
60-
start = tempStart;
61-
step = tempStep;
62-
stop = tempStop;
63-
count = numMatches;
64-
break;
65-
end
66-
end
48+
rangeMatches = ismembc(idealRange, indices);
49+
startInd = find(rangeMatches, 1);
50+
stopInd = find(rangeMatches, 1, 'last');
51+
splitPoints = find(~rangeMatches(startInd:stopInd)) + startInd - 1;
52+
if ~isempty(splitPoints)
53+
subStarts = [startInd (splitPoints + 1)];
54+
subStops = [(splitPoints - 1) stopInd];
55+
[~, largestSegInd] = max(subStops - subStarts + 1, [], 'linear');
56+
startInd = subStarts(largestSegInd);
57+
stopInd = subStops(largestSegInd);
58+
end
59+
subCount = sum(rangeMatches(startInd:stopInd));
60+
if subCount > count
61+
start = idealRange(startInd);
62+
stop = idealRange(stopInd);
63+
step = tempStep;
64+
count = subCount;
6765
end
6866
end
6967
optimalBlock = Block('start', start, 'step', step, 'stop', stop);

0 commit comments

Comments
 (0)