function [U, deter]=meg(A)
n=size(A,1);
deter=1;
for k=1:1:n-1
    [piv, j]=max(abs(A(k:n,k)));
    if (piv==0)
       deter=0;
       return
    end
    if (j ~= 1)
       temp=A(j+k-1,:);
       A(j+k-1,:)=A(k,:);
       A(k,:)=temp;
       deter=-deter;
    end
    deter=deter*A(k,k);
    for i=k+1:1:n
        m(i,k)=A(i,k)/A(k,k);
        A(i,k)=0;
        for j=k+1:1:n
            A(i,j)=A(i,j)-m(i,k)*A(k,j);
        end
    end
end
deter=deter*A(n,n);
U=A;
