odiag {demogR} | R Documentation |
Utility function that creates a square matrix with a vector on the off-diagonal or extracts the specified off-diagonal vector.
odiag(A, at = 0)
A |
either a vector or a matrix |
at |
which diagonal? |
If the first argument is a vector of length k, the result will be a
square matrix of rank k+at with the provided vector along the 'at'
diagonal. Positive values for 'at' place the vector above the
diagonal, negative values below the diagonal, and at=0
(the
default) places the vector on the diagonal.
A vector if argument A
is a matrix and a matrix if A
is
a vector.
odiag
is meant to capture some of the functionality of the
MATLAB function diag().
## Construct a matrix from a vector ## random survival probabilities with mean 0.9 and variance 0.0082 y <- rbeta(4,9,1) A <- odiag(y,-1) ## add fertilities F <- c(0,rep(1,4)) A[1,] <- F ## Extract a vector from a matrix A <- matrix(rnorm(25), nr=5, nc=5) odiag(A,2)