Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

tri

Triangular Matrices (Matlab Style)


Description

Extract lower or upper triangular part of a matrix.

Usage

tril(M, k = 0)
triu(M, k = 0)

Arguments

M

numeric matrix.

k

integer, indicating a secondary diagonal.

Details

tril
Returns the elements on and below the kth diagonal of X, where k = 0 is the main diagonal, k > 0 is above the main diagonal, and k < 0 is below the main diagonal.

triu
Returns the elements on and above the kth diagonal of X, where k = 0 is the main diagonal, k > 0 is above the main diagonal, and k < 0 is below the main diagonal.

Value

Matrix the same size as the input matrix.

Note

For k==0 it is simply an application of the R functions lower.tri resp. upper.tri.

See Also

Examples

tril(ones(4,4), +1)
#    1  1  0  0
#    1  1  1  0
#    1  1  1  1
#    1  1  1  1

triu(ones(4,4), -1)
#    1  1  1  1
#    1  1  1  1
#    0  1  1  1
#    0  0  1  1

pracma

Practical Numerical Math Functions

v2.3.3
GPL (>= 3)
Authors
Hans W. Borchers [aut, cre]
Initial release
2021-01-22

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.