|
39 | 39 | start = 1; |
40 | 40 | step = 1; |
41 | 41 | 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 |
46 | 46 | break; |
47 | 47 | 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; |
67 | 65 | end |
68 | 66 | end |
69 | 67 | optimalBlock = Block('start', start, 'step', step, 'stop', stop); |
|
0 commit comments