%% Sum of Matrix Diagonal
% Create a 3-by-3 matrix and calculate the sum of the diagonal elements.

A = [1 -5 2; 
    -3  7 9; 
     4 -1 6];

b = trace(A)

%
% The result \textrm{tr}\left(A\right)=14 agrees with a manual calculation.
% A=\left\lbrack \begin{array}{ccc}
% a_{11}  & a_{12}  & a_{13} \\
% a_{21}  & a_{22}  & a_{23} \\
% a_{31}  & a_{32}  & a_{33}
% \end{array}\right\rbrack =\left\lbrack \begin{array}{ccc}
% 1 & -5 & 2\\
% -3 & 7 & 9\\
% 4 & -1 & 6
% \end{array}\right\rbrack ,
% \textrm{tr}\left(A\right)=\sum_{i=1}^3 a_{\textrm{ii}} =a_{11} +a_{22} +a_{33} =1+7+6=14\ldotp
% Copyright 2018 The MathWorks, Inc.