Extracts or modifies sub- or super- diagonals of a matrix.

sdiag(A,k=0)
sdiag(A, k = 0) <- value

Arguments

A

a matrix

k

sub- (negative) or super- (positive) diagonal of a matrix. 0 is the leading diagonal.

value

single value, or vector of the same length as the diagonal.

Value

A vector containing the requested diagonal, or a matrix with the requested diagonal replaced by value.

Author

Simon N. Wood simon.wood@r-project.org

Examples

require(mgcv)
A <- matrix(1:35,7,5)
A
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    1    8   15   22   29
#> [2,]    2    9   16   23   30
#> [3,]    3   10   17   24   31
#> [4,]    4   11   18   25   32
#> [5,]    5   12   19   26   33
#> [6,]    6   13   20   27   34
#> [7,]    7   14   21   28   35
sdiag(A,1) ## first super diagonal
#> [1]  8 16 24 32
sdiag(A,-1) ## first sub diagonal
#> [1]  2 10 18 26 34

sdiag(A) <- 1 ## leading diagonal set to 1
sdiag(A,3) <- c(-1,-2) ## set 3rd super diagonal