Skip to content

Instantly share code, notes, and snippets.

@02015678
Created January 20, 2015 16:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 02015678/353ea94c27e4c5f89c9d to your computer and use it in GitHub Desktop.
Save 02015678/353ea94c27e4c5f89c9d to your computer and use it in GitHub Desktop.
Matlab program for LU Factorization with partial (row) pivoting
function [L,U,P]=LU_pivot(A)
% LU factorization with partial (row) pivoting
% K. Ming Leung, 02/05/03
[n,n]=size(A);
L=eye(n); P=L; U=A;
for k=1:n
[pivot m]=max(abs(U(k:n,k)));
m=m+k-1;
if m~=k
% interchange rows m and k in U
temp=U(k,:);
U(k,:)=U(m,:);
U(m,:)=temp;
% interchange rows m and k in P
temp=P(k,:);
P(k,:)=P(m,:);
P(m,:)=temp;
if k >= 2
temp=L(k,1:k-1);
L(k,1:k-1)=L(m,1:k-1);
L(m,1:k-1)=temp;
end
end
for j=k+1:n
L(j,k)=U(j,k)/U(k,k);
U(j,:)=U(j,:)-L(j,k)*U(k,:);
end
end
@Mr-Jef
Copy link

Mr-Jef commented Feb 12, 2022

Hello. I have a question. why you changed "L" matrix after k=1 ??
if k >= 2
temp=L(k,1:k-1);
L(k,1:k-1)=L(m,1:k-1);
L(m,1:k-1)=temp;
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment