% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% Function that compares two different sets of data, using the formula:    %
%                       ( | data1 - data2 | )                              %
%           result = sum(-------------------)                              %
%                       (  size of measures )                              %
% This formula will give us the average deviation from an expected         %
% value.                                                                   %
%                                                                          %
%                                                                          %
% result = function_compareResults(data1, data2)                           %
%  INPUTS:   Name           | Type    | Dimensions | Description           %
%            data1          | measure | nx2        | ground truth          %
%            data2          | measure | mx2        | measures              %
%                                                                          %
% OTUPUTS:   Name           | Type    | Dimensions | Description           %
%            result         | int     | 1x2        | deviation and MaxError%
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %

function result = function_compareResults(data1, data2)
    % The first thing we need to do is establish a correspondecy between
    % the different size data. For that we merge the results into an
    % overall result. Notice that the data with less measures will adjust
    % its time to the data with more results (aproximation of time) and in
    % the times that there is no data, the value of it inexistence will be
    % -60140.
    data = function_assignData(data1, data2);

    % Now we can apply the formula:
    % Notice that:
    %               data(:,1) represents the time
    %               data(:,2) represents exp_cart (ground truth)
    %               data(:,3) represents whatever we measured
    i      = 0;
    result(1) = 0;  % mean absolute difference
    result(2) = 0;  % max error
    result(3) = 0;  % location of error
    for a = 1:size(data,1)  % For every measure
        if (data(a,3) ~= -60140)
            i = i + 1;      % Total amount of data measured
            result(1) = result(1) + abs(data(a,2) - data(a,3));
            
            % This condition will give us the max error existing in the
            % data (for comparation purposes)
            if(abs(data(a,2) - data(a,3)) > result(2))
                result(2) = abs(data(a,2) - data(a,3));
                result(3) = a;
            end
        end
    end
    result(1) = result(1) / i;
end