%% Save Memory with Permutation Vectors
% Compute the LU factorization of a matrix. Save memory by returning the row permutations as a vector instead of a matrix.
%
% Create a 1000-by-1000 random matrix.

A = rand(1000);

%
% Compute the LU factorization with the permutation information stored as a matrix P. Compare the result with the permutation information stored as a vector p. The larger the matrix, the more memory efficient it is to use a permutation vector.

[L1,U1,P] = lu(A);
[L2,U2,p] = lu(A,'vector');
whos P p

%
% Using a permutation vector also saves on execution time in subsequent operations. For instance, you can use the previous LU factorizations to solve a linear system \textrm{Ax}=b. Although the solutions obtained from the permutation vector and permutation matrix are equivalent (up to roundoff), the solution using the permutation vector typically requires a little less time.
% Copyright 2018 The MathWorks, Inc.