diag#
Create a diagonal matrix or retrieve the diagonal of a matrix. When x is a vector, a matrix with the vector values on the diagonal will be returned. When x is a matrix, a vector with the diagonal values of the matrix is returned. When k is provided, the k-th diagonal will be filled in or retrieved, if k is positive, the values are placed on the super diagonal. When k is negative, the values are placed on the sub diagonal.
Available in: math cells, REPL.
Syntax#
diag(x)
diag(x, k)
Arguments and behavior#
For matrices, dimensions must match the operation. Singular matrices or incompatible shapes can fail. Cell indexing starts at 1. The example returns matrix.
Example#
Edit the inputs, then use Run or Reset. Changes stay in this example.
diag(1:3)Interactive examples could not load. You can still copy the expressions.
Expected result: matrix.
See also#
concat · det · identity · inv · ones · range · size · squeeze · subset · trace · transpose · zeros