%% Singular Value Decomposition of Sparse Matrix
% west0479 is a real-valued 479-by-479 sparse matrix. The matrix has a few large singular values, and many small singular values.
% Load west0479 and store it as A.

load west0479
A = west0479;

%
% Compute the singular value decomposition of A, returning the six largest singular values and the corresponding singular vectors. Specify a fourth output argument to check convergence of the singular values.

[U,S,V,cflag] = svds(A);
cflag

%
% cflag indicates that all of the singular values converged. The singular values are on the diagonal of the output matrix S.

s = diag(S)

%
% Check the results by computing the full singular value decomposition of A. Convert A to a full matrix and use svd.

[U1,S1,V1] = svd(full(A));

%
% Plot the six largest singular values of A computed by svd and svds using a logarithmic scale.

s2 = diag(S1);
semilogy(s2(1:6),'r.')
hold on
semilogy(s,'ro','MarkerSize',10)
title('Singular Values of west0479')
legend('svd','svds')

% Copyright 2012 The MathWorks, Inc.