Contents Previous Next Subchapters Current Chapters-> identity zeros ones fill fillcols fillrows seq toeplitz vander hankel hilb invhilb diag tril triu novalue Parent Chapters-> Omatrix6 elementary specialmatrices diag Search Tools-> contents reference index search

The Diagonal Function
 Syntax `diag(`A```) diag(```A`, `k`)` See Also identity , triu , tril , seq

Description
If A is a vector, `diag` returns a square matrix, of minimal dimension, that is zero except for its k-th diagonal which is equal to A. If A is a matrix, `diag` returns the k-th diagonal of the matrix A. If the argument k is not present, the value zero is used in its place. ``` ```The argument k must be integer, real or double-precision. If `k >= 0`, the k-th diagonal of a matrix `B` is the column vector consisting of the sequence of elements `B(1, 1+k)`, `B(2, 2+k)`, ... , where the sequence continues as long as both indices are within the matrix. If `k <= 0`, the k-th diagonal of a matrix `B` is the column vector consisting of the sequence of elements `B(1-k, 1)`, `B(2-k, 2)`, ... , where the sequence continues as long as both indices are within the matrix.

Example: Vector Argument
If you enter ```      A = {1, 2}      k = 1      diag(A, k) ``` O-Matrix will respond ```      {      [ 0 , 1 , 0 ]      [ 0 , 0 , 2 ]      [ 0 , 0 , 0 ]      } ```
Example: Matrix Argument
If you enter ```      A = {[1, 2, 3] , [4, 5, 6], [7, 8, 9]}      k = -1      diag(A, k) ``` O-Matrix will respond ```      {      4      8      } ```