Skip to content

triu

Upper triangular part of a matrix

triu(X)

  • X should be a matrix, i.e., ndims(X) equal to 2.
  • It returns the upper triangular part of X, i.e., elements above and on the main diagonal of X.
  • If X is a character array, elements below the main diagonal are filled with the white-space character.
  • It returns an empty matrix if X is empty.

triu(X, k)

  • k should be an integer scalar, not equal to NaN or Inf.
  • If k < 0, it returns elements above and on the lower k-th diagonal of X.
  • If k > 0, it returns elements above and on the upper k-th diagonal of X.
  • If k == 0, it returns the same result as triu(X).

Example 1: In the following, triu() extracts the upper triangular part of a character array. The lower part is filled with spaces.

a=['abcd';'efgh';'ijkl';'mnop']
triu(a)
a =
abcd
efgh
ijkl
mnop

ans =
abcd
 fgh
  kl
   p