% Cesar sousa 8-10-2014

%join 2 organized matrrixes ordered by the column 'col'

function output = joinMatrix(A,B,col)

    % verify if the matrixes have the same number of columns 

    %create a matrix that will contain all the values
    output = zeros(size(A,1)+size(B,1),size(A,2));
    
    contA=1;
    contB=1;
    A;
    B;
    B_chegou_fim=0;
    A_chegou_fim=0;
    
    for i=1:size(output,1)
        if (A(contA,col) <= B(contB,col) || B_chegou_fim == 1) && A_chegou_fim ==0
            output(i,:) = A(contA,:);
            contA = contA +1;
            if size(A,1) < contA
                contA = contA -1;
                A_chegou_fim = 1;
            end
        else
            output(i,:) = B(contB,:);
            contB = contB +1;
            if size(B,1) < contB
                contB = contB - 1;
                B_chegou_fim = 1;
            end
        end

    end

end