clc clear all % trace of matrix disp('Example 1 - Matrix ops') disp('Trace of a matrix') % add all the diagonal elements together % INPUTS a=[2 -1 3;4 1 3;5 7 -4]; [r,c]=size(a); disp('The input matrix is:') disp(a) % CODE if r~=c disp('Error - matrix is not square') else trace=0; for i=1:1:r trace=trace+a(i,i); end fprintf('The trace is %g\n',trace) end