function accuracy=clustering_accuracy(ytrue,ypred)
% function accuracy=clustering_accuracy(ytrue,ypred)
%
% Find the clustering accuracy of prediction, given the true labels
%
% INPUT
% ytrue = a vector of true labels
% ypred = a vector of the predicted labels

% OUTPUT
% acc = Accuracy of clustering results
%
% (C) 2021 Moo K. Chung
%     University of Wisconsin-Madison
%  Contact mkchung@wisc.edu for support 
%
% Update history
%   2021 Nov 28 



%test data
%ytrue = [ 1 1 1  2 2 3 3]
%ypred = [ 1 1 2  1 1 3 3]

n=length(ytrue); % number of samples

%confusion matrix
C = confusionmat(ytrue,ypred); 

%The iteration below also computes the confusion matrix
%k=max([ytrue(:); ypred(:)]) %find # of cluster
%C=zeros(k);
%for i=1:n  
%    C(ypred(i),ytrue(i))=C(ypred(i),ytrue(i))+1;
%end

%test example
%2     2     0
%1     0     0
%0     0     2
     

%solve the linear assignment problem of maximizing the clusting result
% 0 is the cost of mismatch. We ony care matched pairs
M=matchpairs(C, 0, 'max'); 

% test example
% 2 1 
% 1 2
% 3 3

accuracy=sum(C(sub2ind(size(C), M(:,1), M(:,2))))/n;

