%% Extract Specific Diagonals from Matrix
%
% Create a 5-by-5 random matrix.

A = randi(10,5,5)

%
% Extract the main diagonal, and the first diagonals above and below it.

d = [-1 0 1];
Bout = spdiags(A,d)

%
% Try to extract the fifth super-diagonal (d = 5). Because A has only four super-diagonals, spdiags returns the diagonal as all zeros of the same length as the main (d = 0) diagonal.

B5 = spdiags(A,5)

%
% Copyright 2019 The MathWorks, Inc.