Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • variables to matrix and matrix to variables

    Dear all,

    I have 9 variables and for each observation in my dataset I want to:
    1. Create a 3x3 matrix of the following form T=(var1, var2, var3\ var4, var5, var6\ var7, var8, var9)
    2. Calculate the eigenvectors and eigenvalues
      Code:
      	mata:X=.
      	mata:L=.
      	mata:eigensystem(T, X, L)
    3. Perform some additional calculations that in the end will produce a 3x3 matrix
    4. Finally, create 9 new variables that will contain the elements of my final matrix
    I need help with steps 1 and 4.
    Thank you in advance for your help.

    Regards,
    Nikos

  • #2
    Code:
    // start empty and create example dataset
    clear all
    mata: mata clear
    set obs 100
    forvalues i = 1/9 {
        gen var`i' = rnormal()
    }
    
    // start the solution
    // I assume you want to store matrix X, but you probably want to change that
    
    // create vars in which to store the results
    forvalues i = 1/9 {
        gen double res`i' = .
    }
    mata // start Mata
    X=.
    L=.
    for(i=1; i<=st_nobs(); i++) {  // loop over observations
        T = st_data(i,("var1", "var2", "var3",
                       "var4", "var5", "var6",
                       "var7", "var8", "var9")) // load data for observation i
        T = colshape(T,3)   // turn the rowvector in a 3x3 matrix
        eigensystem(T,X,L)  
        X = colshape(Re(X),9)  // turn X into a 1x9 vector of real values
        st_store(i,("res1", "res2", "res3",
                    "res4", "res5", "res6",
                    "res7", "res8", "res9"), X) // store the results
    }
    end
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Dear Maarten,

      thank you very much for the prompt and to the point solution!

      Regards,
      Nikos

      Comment

      Working...
      X