%% Reorder Sparse Matrix with Permutation Vector
% Use the 'vector' option of chol to return the permutation information as a vector rather than a matrix.
%
% Create a sparse finite element matrix.

S = gallery('wathen',10,10);
spy(S)

%
% Calculate the Cholesky factor for the matrix, and specify the 'vector' option to return a permutation vector p.

[R,flag,p] = chol(S,'vector');

%
% Verify that flag = 0, indicating the calculation is successful.

if ~flag
    disp('Factorization successful.')
else
    disp('Factorization failed.')
end

%
% Verify that S(p,p) = R'*R, within roundoff error.

norm(S(p,p) - R'*R,'fro')

%
% Copyright 2018 The MathWorks, Inc.