# How to generate the matrix $\mathbf{M}$ with $\operatorname{cov}(\mathbf{M})=\mathbf{I}$

How can I generate a random matrix $\mathbf{M}$ such that $\operatorname{cov}(\mathbf{M}) = \mathbf{I}$ (identity). I use matlab to generate $\mathbf{M}$.

• mvrnd(zeros(d), eye(d)) – valtron Jan 21 '13 at 16:32

Hmm, I don't have matlab, but a sequence of my MatMatecode might be translatable:

n=1000    // 1000 cases
v=10      // 10 variables

data = randomn(v,n,0,2)   // generate random data of stddev 2,mean=0
// this shall have some random-correlation

c1=data *' / n            // covariance-matrix of the data
t1=gettrans(c1 ,"pca")'   // this mimicks SVD-decomposition; the matrix t1 is
// the left one of the S D V - matrices and
//      is orthogonal. Note: the apostroph-symbol means
//      transposition
data1 = t1 * data         // t1 * data gives uncorrelated data as can be seen
c2 = data1  *' / n        //   since the covariance c2 is diagonal

• What do you mean of "t1=gettrans(c1 ,"pca")'", Do you mean I must do PCA on the covariance matrix? – remo Jan 21 '13 at 16:30
• @remo: it gives the rotation-matrix, which rotates (columnwise) any matrix to "pca"-position. In this case, where c1 is symmetric it is as well as rotating the rows (which is what we want). I think in matlab this should be implemented as SVD-procedure, which gives two rotation-matrices and the diagonal for a SVD-decomposition. Again - as c1 is symmetric, the first of the three SVD-matrices should be identical to my matrix t1 here (maybe transposed) – Gottfried Helms Jan 21 '13 at 16:37
• ... adding explanation: the term "pca-position" means here, that the columns are rotated such that they become mutually orthogonal. If you use that t1 then as rotation for the rows of "data", the rows of "data" become mutually orthogonal - thus zero-correlated – Gottfried Helms Jan 21 '13 at 16:40