%% Reduce Fill-in of Sparse Matrix Factorization
% Compare the results of computing the LU factorization of a sparse matrix with and without column permutations.
%
% Load the west0479 matrix, which is a real-valued 479-by-479 sparse matrix.

load west0479
A = west0479;

%
% Calculate the LU factorization of A by calling lu with three outputs. Generate spy plots of the L and U factors.

[L,U,P] = lu(A);
subplot(1,2,1)
spy(L)
title('L factor')
subplot(1,2,2)
spy(U)
title('U factor')

%
% Now, calculate the LU factorization of A using lu with four outputs, which permutes the columns of A to reduce the number of nonzeros in the factors. The resulting factors are much sparser than if column permutations are not used.

[L,U,P,Q] = lu(A);
subplot(1,2,1)
spy(L)
title('L factor')
subplot(1,2,2)
spy(U)
title('U factor')

% Copyright 2018 The MathWorks, Inc.