function distance=kldist(rd,pd)
%KLDIST	  Calculate the average Kullback-Leibler divergence between the predicted label
%         distribution and the real label distribution.
%
%	Description
%   DISTANCE = KLDIST(RD, PD) calculate the average Kullback-Leibler divergence 
%   between the predicted label distribution and the real label distribution.
%
%   Inputs,
%       RD: real label distribution
%       PD: predicted label distribution
%
%   Outputs,
%       DISTANCE: Kullback-Leibler divergence
%
%	See also
%	EUCLIDEANDIST, SQUAREDXDIST, SORENSENDIST
%	
%
[rows,cols]=size(rd); % Size of rd, rows*cols
for i=1:rows
    dist(i)=0;
    for j=1:cols
        if(rd(i,j)<=0||pd(i,j)<=0)
            dist(i)=dist(i);
        else
            dist(i)=dist(i)+rd(i,j)*log(rd(i,j)/pd(i,j));
        end
    end
end
totalDist=0;
for i=1:rows
    totalDist=totalDist+dist(i);
end
averageDist=totalDist/rows; % Average distance 
distance=averageDist;
end
