Learn R Programming

genscore (version 1.0.2.2)

ran_mat: Random generator of matrices with given eigenvalues.

Description

Random generator of matrices with given eigenvalues.

Usage

ran_mat(p, ev = stats::runif(p, 0, 10), seed = NULL)

Value

A p by p matrix whose eigenvalues equal to ev.

Arguments

p

A positive integer >= 2, the dimension.

ev

A vector of length p, the eigenvalues of the output matrix.

seed

A number, the seed for the generator. Ignored if NULL.

Details

The function randomly fills a p by p matrix with independent \(Normal(0,1)\) entries, takes the Q matrix from its QR decomposition, and returns \(Q' diag(ev) Q\).

Examples

Run this code
p <- 30
eigen_values <- (0.1*p-1+1:p)/p
K <- ran_mat(p, ev=eigen_values, seed=1)
sort(eigen(K)$val)-eigen_values

Run the code above in your browser using DataLab