clear; close all; 
load 'locallyLearnedTemplate'; % load in scores on positive examples

cut = 80; 
numTemplate = 30; % number of selected templates 
MAX2score = zeros(numImage, 1); % max score for nearest neighbor classification
MAX2scoreToBe = zeros(numImage, 1); % tentative max score by adding a new template
selectedTemplate = zeros(numTemplate, 1); % indices of selected templates
for (k = 1:numTemplate)
  for (this = 1:numImage) % go over all the templates
    for (i = 1:numImage) % computing tentative nearest neighbor score
        MAX2scoreToBe(i) = max(MAX2score(i), allSUM2score(this, i)); 
    end
    propose(this) = sum(MAX2scoreToBe.*(MAX2scoreToBe>cut)); 
  end
 [maxPropose, maxTemplate] = max(propose);  % find the template that increases the margin the most 
  for (i = 1:numImage)  % update nearest neighbor score
        MAX2score(i) = max(MAX2score(i), allSUM2score(maxTemplate, i)); 
  end
  selectedTemplate(k) = maxTemplate; % record the index of the selected template
end
outTemplateSelected; % output the selected template
selectedTemplate
save 'SequentialSelectionResult' numTemplate selectedTemplate cut;