matlab实现矩阵LU分解

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%matrix LU Factorize
%authour: j.cai
%mail: [email protected]
%time: 2016-09-29
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [L,U,P]= LU(A)
    disp("LU factorization");
    L=zeros(size(A));
    U=zeros(size(A));
    P=zeros(size(A));
    if size(A,1)~=size(A,2)
        disp("不是方阵");
        return 
    end
    n=size(A,1);
    seqence=linspace(1,n,n);
    A=[A,seqence']; %加上行号 
    for c=1:n %部分主元法 找主元
        [~,pivotnum]=max(abs(A(c:end,c)));
        pivotnum=pivotnum+c-1;
        if pivotnum ~= c %交换行 
            row_temp=A(c,:);
            A(c,:)=A(pivotnum,:);
            A(pivotnum,:)=row_temp;
        end
        for r=(c+1):n %消元 
            if abs(A(c,c))

你可能感兴趣的:(matlab,矩阵)