Navigation

Operators and Keywords

Function List:

C++ API

: M = diag (v)
: M = diag (v, k)
: M = diag (v, m, n)
: v = diag (M)
: v = diag (M, k)

Return a diagonal matrix with vector v on diagonal k.

The second argument is optional. If it is positive, the vector is placed on the k-th superdiagonal. If it is negative, it is placed on the -k-th subdiagonal. The default value of k is 0, and the vector is placed on the main diagonal. For example:

diag ([1, 2, 3], 1)
  ⇒  0  1  0  0
      0  0  2  0
      0  0  0  3
      0  0  0  0

The 3-input form returns a diagonal matrix with vector v on the main diagonal and the resulting matrix being of size m rows x n columns.

Given a matrix argument, instead of a vector, diag extracts the k-th diagonal of the matrix.

Package: octave