%% Get Diagonal Elements
% Get the elements on the main diagonal of a random 6-by-6 matrix.

A = randi(10,6)

%

x = diag(A)

%
% Get the elements on the first subdiagonal (k=-1) of A. The result has one fewer element than the main diagonal.

x1 = diag(A,-1)

%
% Calling diag twice returns a diagonal matrix composed of the diagonal elements of the original matrix.

A1 = diag(diag(A))

% Copyright 2012 The MathWorks, Inc.