function [popIndexSorted maxLabelsToAsk] = findPopularity(neighborMatrix)

% find how many times every instance occurs as the closest neighbor to an
% instances other than themselves (hence ==2 in the find function)
% this will be the first popIndex
n = size(neighborMatrix,1);
popIndex = zeros(1,n);
for i = 1:n
    popIndex(i) = size(find(neighborMatrix(:,i)==2),1);
    if popIndex(i) == 0         % if its not closest to anyone then drop its popularity
        popIndex(i) = -999999;
    end
end

maxLabelsToAsk = size(find(popIndex > 0),2);
[tmp popIndexSorted] = sort(popIndex, 'descend');

end