Matlab-Matrix - Trace



Trace helps you to calculate the sum of diagonal elements in a given matrix.

Example

Consider the given 3x3 matrix. Let us find out the sum of diagonal elements as shown below −

a = [ 1 2 3; 2 3 4; 1 2 5];
test = trace(a)

Output

The execution in MATLAB is as follows −

>> a = [ 1 2 3; 2 3 4; 1 2 5]
test = trace(a)

a =

   1  2  3
   2  3  4
   1  2  5
 
test =

   9
   
>>
Advertisements