Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Generating covariance matrices as defined in MATLAB

  1. Jul 12, 2012 #1
    Hi, I'm fairly new to matlab and I was wondering if you guys could help me out. If I have an N*N matrix, C where the (k,l)-entry is defined as:
    http://a3.sphotos.ak.fbcdn.net/hphotos-ak-ash3/556394_10151031836051952_2120388553_n.jpg [Broken]

    Where x_i is from an N-vector where x_i is normally distributed with 0 mean and variance 1. So x_i ~ N(0,1).
    And so the vector, x= [x_1 x_2 ... x_N] = [ randn randn ... rand]

    For the case N=3, we have
    http://a5.sphotos.ak.fbcdn.net/hphotos-ak-ash3/532427_10151031836071952_2140805136_n.jpg [Broken]

    and x= [x_1 x_2 x_2] = [randn randn randn].

    So the (1,2)-entry of C, is: http://a2.sphotos.ak.fbcdn.net/hphotos-ak-snc7/417729_10151031836006952_1450809886_n.jpg [Broken]
    which equals (if worked out) (1/3)*[(x_1)*(x_2) + (x_2)*(x_3)]

    We can work out all the other entries of C and then get a 3*3 matrix.

    All of this I can do my hand but what I want Matlab to do this task but for N=30.

    So here x=[x_1 x_2 ... x_30] = [randn randn ... randn]
    I need a code to generate my 30*30 matrix C from such set x_i's with the defintion above (N replaced by 30 of course). Does anyone know how I go about doing this?

    EDIT: If it helps at all, the diagonal entries are always the same, it is always (1/N)*[(x_1)^2 + (x_2)^2 + ... + (x_N)^2]
    and the matrix, C is always symmetric. I would appreciate any help on this; I need to generate these random matrices for a maths project of mine.
    Last edited by a moderator: May 6, 2017
  2. jcsd
  3. Jul 14, 2012 #2


    User Avatar
    Gold Member

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook