function [A,b] = pivot(k,A,b) [n,m] = size(A); % first find where the largest entry is at or below A(k,k) i_piv = k; piv_max = abs(A(k,k)); for i=k+1:n if abs(A(i,k)) > piv_max i_piv = i; piv_max = abs(A(i,k)); end end % now that we know where the largest entry is, we swap the rows of A: temp = A(k,:); A(k,:) = A(i_piv,:); A(i_piv,:) = temp; % and we swap the entries of b: temp = b(k); b(k) = b(i_piv); b(i_piv) = temp;