Navigation

Operators and Keywords

Function List:

C++ API

: tril (A)
: tril (A, k)
: tril (A, k, pack)
: triu (A)
: triu (A, k)
: triu (A, k, pack)

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.

If the value of k is nonzero integer, the selection of elements starts at an offset of k diagonals above or below the main diagonal; above for positive k and below for negative k.

The absolute value of k must not be greater than the number of subdiagonals or superdiagonals.

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

If the option "pack" is given as third argument, the extracted elements are not inserted into a matrix, but rather stacked column-wise one above other.

See also: diag.

Package: octave