How to extract the upper triangular matrix in Matlab and Octave
To extract the upper triangular matrix from a matrix in Matlab and Octave, use the function triu()
triu(M)
The M parameter is a matrix (array).
The triu() function outputs an upper triangular matrix.
What is an upper triangular matrix? It is a matrix in which all values under the main diagonal are null. Non-null values are on the main diagonal and above the main diagonal. For example $$ T = \begin{pmatrix} 1 & 2 & 3 \\ 0 & 5 & 6 \\ 0 & 0 & 9 \end{pmatrix} $$
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
Extract the upper triangular matrix using the command triu(M)
>> triu(M)
ans =
1 2 3
0 5 6
0 0 9
The triu() function displays the upper triangular matrix of matrix M.