function [x,A,p]=gausspivot(A,b)
n=length(b);
p=[1:n];
for k=1:n-1
    m=max(abs(A(k:n,k)));
    s=find(abs(A(k:n,k))==m);
    r=s(1)+k-1;
    if r ~= k
        aux=p(r); p(r)=p(k); p(k)=aux;
        aux=A(r,:); A(r,:)=A(k,:); A(k,:)=aux;
        aux=b(r); b(r)=b(k); b(k)=aux;
    end
    A(k+1:n,k)=A(k+1:n,k)/A(k,k);
    A(k+1:n,k+1:n)=A(k+1:n,k+1:n)-A(k+1:n,k)*A(k,k+1:n);
    b(k+1:n)=b(k+1:n)-A(k+1:n,k)*b(k);
end
x=backward(A,b);