Skip to content

triu

Upper-triangular part of a matrix.

L = triu(X)
L = triu(X, k)

Returns a matrix the same size as X containing only the upper-triangular elements; everything below the main diagonal is zeroed. With the optional k offset, shifts the boundary: positive k removes k superdiagonals; negative k keeps |k| subdiagonals.

triu(magic(4)) % keep main diagonal and above
triu(magic(4), 1) % strict upper (zero out main diagonal too)
  • tril — Lower-triangular part of a matrix.
  • diag — Extract a diagonal, or build a matrix from a diagonal.