diag
Creates a diagonal matrix from a sequence of numbers.
Syntax

diag(a[, [b, ...]])

a, b, ...
are numbers


diag(X)

X
is a vector or a list of numbers

Description

If
a, b, ...
are numbers, thendiag(a, b, ...)
is the diagonal matrix havinga, b, ...
on the diagonal. 
If
X
is a vector or a list of numbers, thendiag(X)
is the diagonal matrix having the elements ofX
on the diagonal.
Examples
diag(4, 1, 2, 1)
⎛4 0 0 0⎞ ⎜0 1 0 0⎟ ⎜0 0 2 0⎟ ⎝0 0 0 1⎠
diag(❨7, 2, 1, 1, −2❩)
⎛ 7 0 0 0 0⎞ ⎜ 0 2 0 0 0⎟ ⎜ 0 0 1 0 0⎟ ⎜ 0 0 0 1 0⎟ ⎝ 0 0 0 0 −2⎠
diag('(0, 1, 2, 1, 0, 1))
⎛0 0 0 0 0 0⎞ ⎜0 1 0 0 0 0⎟ ⎜0 0 2 0 0 0⎟ ⎜0 0 0 1 0 0⎟ ⎜0 0 0 0 0 0⎟ ⎝0 0 0 0 0 1⎠
See also

Creating a matrix (conceptual)