
m = 5; n = 3;
X = [1:m]'*ones(1,n)+ones(m,1)*[0:n-1]
T = diag(X(:,2))+ diag(X(1:m-1,1),-1)+diag(X(2:m,3),1)
S1 = sparse(T);
%%-------------------- using spdiags:
S2 = spdiags(X,[-1:1],m,m) 
aa = [X(1:m-1,1); X(1:m,2); X(2:m,3)]
ii = [2:m, 1:m, 1:m-1]'
jj = [1:m-1, 1:m, 2:m]'
S3 = sparse(ii, jj, aa, m,m)
S4 = spconvert([ii, jj, aa]) 
