function [precision,recall,f_measure] = evaluate_matrix1_matrix2(matrix_retrieved,matrix_relevant,parameter)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Name: evaluate_matrix1_matrix2
% Programmer: Meinard Müller, Verena Konz
%
%
% Input: matrix_retrieved
%        matrix_relevant  
%
%        parameter
%
% Output: precision
%         recall
%         f_measure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin<3
   parameter=[]; 
end

if isfield(parameter,'neglectNotAnnotated')==0
   parameter.neglectNotAnnotated = 0;
end

if isfield(parameter,'fprintf')==0
   parameter.fprintf = 0;
end

if isfield(parameter,'visualize')==0
   parameter.visualize = 0;
end

if isfield(parameter,'position')==0
   parameter.position = -1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Main
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if parameter.neglectNotAnnotated==1
    for k = 1: size(matrix_relevant,2)
        if sum(matrix_relevant(:,k))==0
            matrix_retrieved(:,k) = zeros(24,1);
        end
    end
end

% precision = |({relevant} and {retrieved})/{retrieved}|
numRetrieved = sum(sum(matrix_retrieved));
if numRetrieved>0
    precision = sum(sum(matrix_relevant .* matrix_retrieved))/numRetrieved;
else
    precision = 0;
end

% recall = |({relevant} and {retrieved})/{relevant}|
numRelevant = sum(sum(matrix_relevant));
if numRelevant > 0
    recall = sum(sum(matrix_relevant .* matrix_retrieved))/numRelevant;
else
    recall = 0;
end

% F-Measure
if ((precision+recall) == 0)
    f_measure = 0;
else
    f_measure = 2* (precision * recall)/ (precision + recall);
end


if parameter.fprintf == 1
    fprintf('P=%5.3f, R=%5.3f, F=%5.3f \n ',precision,recall,f_measure);
end

if parameter.visualize == 1
    visualize_chordMatrix(matrix_retrieved,matrix_relevant,parameter);
    if parameter.position(1) ~= -1
        set(gcf,'Position',parameter.position);
    end
end


