odiag {demogR} | R Documentation |
odiag
Description
Utility function that creates a square matrix with a vector on the off-diagonal or extracts the specified off-diagonal vector.
Usage
odiag(A, at = 0)
Arguments
A |
either a vector or a matrix |
at |
which diagonal? |
Details
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.
Value
A vector if argument A
is a matrix and a matrix if A
is
a vector.
Note
odiag
is meant to capture some of the functionality of the
MATLAB function diag().
See Also
Examples
## 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)
[Package demogR version 0.6.0 Index]