%% Create Tridiagonal Matrix
% Create a tridiagonal matrix using three vectors, change some of the matrix diagonals, and then extract the diagonals.
%
% Create a 9-by-1 vector of ones, and then create a tridiagonal matrix using the vector. View the matrix elements.

n = 9;
e = ones(n,1);
A = spdiags([e -2*e e],-1:1,n,n);
full(A)

%
% Change the values on the main (d = 0) diagonal of A.

Bin = abs(-(n-1)/2:(n-1)/2)';
d = 0;
A = spdiags(Bin,d,A);
full(A)

%
% Finally, recover the diagonals of A as the columns in a matrix.

Bout = spdiags(A);
full(Bout)

% Copyright 2019 The MathWorks, Inc.