Learn R Programming

pracma (version 1.5.8)

Diag: Matrix Diagonal

Description

Generate diagonal matrices or return diagonal of a matrix

Usage

Diag(x, k = 0)

Arguments

x
vector or matrix
k
integer indicating a secondary diagonal

Value

  • matrix or vector

Details

If x is a vector, Diag(x, k) generates a matrix with x as the (k-th secondary) diagonal.

If x is a matrix, Diag(x, k) returns the (k-th secondary) diagonal of x.

The k-th secondary diagonal is above the main diagonal for k > 0 and below the main diagonal for k < 0.

See Also

diag, Trace

Examples

Run this code
Diag(matrix(1:12,3,4),  1)
Diag(matrix(1:12,3,4), -1)

Diag(c(1,5,9), 1)
Diag(c(1,5,9), -1)

Run the code above in your browser using DataLab