lettura simple

Trace of a matrix in Octave

In this lesson I'll explain how to calculate the trace of a matrix in Octave

What is a matrix trace? The trace of a matrix is the sum of the elements on the main diagonal of the matrix. For example, this is a 3x3 matrix $$ M= \begin{pmatrix} \color{red}1 & 2 & 3 \\ 4 & \color{red}5 & 6 \\ 7 & 8 & \color{red}9 \end{pmatrix} $$ The trace of the matrix is 15 $$ TR(M) = 1 + 5 + 9 = 15 $$

I'll give you a practical example.

Create a 3x3 square matrix with three rows and three columns.

>> M = [ 1 2 3 ; 4 5 6 ; 7 8 9 ]
M =
1 2 3
4 5 6
7 8 9

Type trace (M) to get the trace of the M matrix

>> trace(M)
ans = 15

In this case the trace of the matrix is 15.

Verify. This is the 3x3 matrix of the example $$ M= \begin{pmatrix} \color{red}1 & 2 & 3 \\ 4 & \color{red}5 & 6 \\ 7 & 8 & \color{red}9 \end{pmatrix} $$ The sum of the elements on the main diagonal of the matrix is 15 $$ TR(M)=1+5+9 = 15 $$

You can also calculate the trace on rectangular matrices in Octave.

For example, define a 2x3 rectangular matrix with two rows and three columns.

>> M = [ 1 2 3 ; 4 5 6 ]
M =
1 2 3
4 5 6

Type trace(M) function to calculate the trace of the M matrix

>> trace(M)
ans = 6

The trace of the matrix is equal to 6

Verify. The sum of the elements on the main diagonal is 6 $$ M= \begin{pmatrix} \color{red}1 & 2 & 3 \\ 4 & \color{red}5 & 6 \end{pmatrix} $$ $$ TR(M)=1+5 = 6 $$

This way you can calculate the trace of any square or rectangular matrix in Octave.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin