function ShowSparse2()
% Set up a large random tridiagonal matrix.
% Makes use of
% S = SPARSE(i,j,s,m,n,nzmax)
% This uses the rows of [i,j,s] to generate an
% m-by-n sparse matrix with space allocated for
% nzmax nonzeros.
clc
n = 10000;
% Method 1. Sets up a random diagonal matrix in sparse
% format and then adds in the off-diagonal entries with a loop.
tic
i = 1:n;
j = 1:n;
s = randn(n,1);
T = sparse(i,j,s,n,n);
for k=1:n-1
T(k+1,k) = randn(1);
T(k,k+1) = randn(1);
end
Method1 = toc;
fprintf('Method 1 Time = %6.3f\n',Method1)

% Method 2. Sets up the complete i, j, and s vectors and then
% calls sparse. This way the sparse data structure is referenced
% only once...
tic
i = [2:n 1:n 1:n-1];
j = [1:n-1 1:n 2:n];
s = randn(3*n-2,1);
T = sparse(i,j,s,n,n);
Method2 = toc;
fprintf('Method 2 Time = %6.3f\n',Method2)