Function Reference
— Function File: tril (a, k)
— Function File: triu (a, k)

Return a new matrix formed by extracting the lower (tril) or upper (triu) triangular part of the matrix a, and setting all other elements to zero. The second argument is optional, and specifies how many diagonals above or below the main diagonal should also be set to zero.

The default value of k is zero, so that triu and tril normally include the main diagonal as part of the result matrix.

If the value of k is negative, additional elements above (for tril) or below (for triu) the main diagonal are also selected.

The absolute value of k must not be greater than the number of sub- or super-diagonals.

For example,

          tril (ones (3), -1)
                 0  0  0
                   1  0  0
                   1  1  0

and

          tril (ones (3), 1)
                 1  1  0
                   1  1  1
                   1  1  1

See also: triu diag