odiag: odiag

Description Usage Arguments Details Value Note See Also Examples

Description

Utility function that creates a square matrix with a vector on the off-diagonal or extracts the specified off-diagonal vector.

Usage

1
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

diag

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## 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)

demogR documentation built on May 1, 2019, 10:56 p.m.