function factors = ChooseTopSimilarityFactors (allFactors, F)
% This function chooses the similarity factors with the highest similarity
% out of all the possibilities.
%
% Input:
%   allFactors: An array of all the similarity factors.
%   F: The number of factors to select.
%
% Output:
%   factors: The F factors out of allFactors for which the similarity score
%     is highest.
%
% Hint: Recall that the similarity score for two images will be in every
%   factor table entry (for those two images' factor) where they are
%   assigned the same character value.
%
% Copyright (C) Daphne Koller, Stanford University, 2012

% If there are fewer than F factors total, just return all of them.
if (length(allFactors) <= F)
    factors = allFactors;
    return;
end

% Your code here:

factors = repmat(struct('var', [], 'card', [], 'val', []), F, 1);

br = [];

for i = 1:length(allFactors)
  br(i) = allFactors(i).val(1);
endfor

br = sort(br, "descend")(1:F);
min_br = min(br);
num_min = sum(br == min_br);
count_min = 0;
count_topF = 1;

for i = 1:length(allFactors)
  if (allFactors(i).val(1) > min_br)
    factors(count_topF) = allFactors(i);
    count_topF += 1;
  elseif (allFactors(i).val(1) == min_br && count_min < num_min)
    factors(count_topF) = allFactors(i);
    count_topF += 1;
    count_min += 1;
  endif
endfor





