function [ M ] = mergevec( A, B )
%MERGEVEC merges vectors A and B into vector M
%  Both input vectors are assumed sorted. The output is sorted.
%  Minas Spetsakis, March 2015

Na = length(A);
Nb = length(B);
N = Na+Nb;
ia=1;
ib=1;
M = zeros(N,1);

for i=1:N
    if (ia<=Na)&(ib<=Nb);
        if A(ia)<=B(ib);
            M(i) = A(ia);
            ia = ia+1;
        else
            M(i) = B(ib);
            ib = ib+1;
        end;
    elseif ia<=Na
        M(i) = A(ia);
        ia = ia+1;
    else
        M(i) = B(ib);
        ib = ib+1;
    end;
    

end

