Announcement

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

  • Creating matrix from vector

    Hi all,

    So I have a vector (421x1) that contains all the variables from the 421x421 matrix. (uploaded as excel file: mat_vector_1990.xlsx)
    For instance.. the matrix below, where n=421.
    a11 a12 .... a1n
    a21 a22 .... a2n
    ....
    an1 an2 .... ann

    However, as the matrix is a non-directional matrix, the upper half is uncoded (lower triangular matrix) and each element was converted into a single column as below:

    a11
    a21
    ...
    an1
    a22
    a32
    ...
    ann

    I've included a picture by hand to explain as well.

    Click image for larger version

Name:	KakaoTalk_20170405_003023289.jpg
Views:	1
Size:	38.5 KB
ID:	1382013

    Now I want to convert the vector to a lower triangular matrix.

    So what I would like to create is, the first column having the first 421 entries of the vector data, second column having the next 420 data entries(first cell missing, though It doesn't matter if it is filled the correct value), third column having 419 data entries.. and so on to create the 421x421 matrix again.

    I googled and searched the forum, but couldn't find solution to how I could work this out. I'm using Stata 14/MP and any hint or help would really be appreciated.

    Thanks in advance!
    John
    Attached Files
    Last edited by Johnathan Kim; 04 Apr 2017, 09:45.

  • #2
    So, first you need to import the data into Stata. See -help import excel- if you are not familiar with it. In any case, I'll assume you have in your data set a variable, I'll call it x, with 421x422/2 observations.

    Code:
    matrix M = J(421, 421, .)
    
    local counter 1
    forvalues j = 1/421 { // LOOP OVER COLUMNS
        forvalues i = `j'/421 { // LOOP OVER ROWS OF TARGET
            matrix M[`i', `j'] = x[`counter']
            local ++counter
        }
    }
    should then create the lower triangular matrix M you are looking for.

    Note: Attaching Microsoft Office documents is discouraged in this forum. Some of the most frequent responders here do not use Office software. Others, like me, do, but will not download an Office document from a stranger because they can contain active malware. The better way to do this would be to import the data into Stata first, and then post an example from the data using the -dataex- command. Please read the FAQ, with special emphasis on #12 for tips on how to maximize your chances of getting helpful, timely responses to your questions.

    Comment


    • #3
      It also sounds like the Mata function -invvech() is close to what you want, and would be worth considering if execution time matters.
      Code:
      matrix v = (1\2\3\4\6\9) // example vector
      mata: st_matrix("M", invvech(st_matrix("v")))
      mat list v
      matrix list M



      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        So, first you need to import the data into Stata. See -help import excel- if you are not familiar with it. In any case, I'll assume you have in your data set a variable, I'll call it x, with 421x422/2 observations.

        Code:
        matrix M = J(421, 421, .)
        
        local counter 1
        forvalues j = 1/421 { // LOOP OVER COLUMNS
        forvalues i = `j'/421 { // LOOP OVER ROWS OF TARGET
        matrix M[`i', `j'] = x[`counter']
        local ++counter
        }
        }
        should then create the lower triangular matrix M you are looking for.

        Note: Attaching Microsoft Office documents is discouraged in this forum. Some of the most frequent responders here do not use Office software. Others, like me, do, but will not download an Office document from a stranger because they can contain active malware. The better way to do this would be to import the data into Stata first, and then post an example from the data using the -dataex- command. Please read the FAQ, with special emphasis on #12 for tips on how to maximize your chances of getting helpful, timely responses to your questions.
        Thank you for your kind reply Clyde. I will check the FAQ and will make sure that I post datasets in .dta format. The code you gave worked perfectly!
        If you don't mind, now I'm trying to make the full matrix out of the lower triangular matrix. i.e matrix M+M', except for the diagonals.
        However, the matrix command initiating the addition only leaves the diagonal as it creates missing values for the upper half. Would there be way to work this out as well?

        Thanks again, you are a savior and saved tremendous amount of time.



        Comment


        • #5
          Originally posted by Mike Lacy View Post
          It also sounds like the Mata function -invvech() is close to what you want, and would be worth considering if execution time matters.
          Code:
          matrix v = (1\2\3\4\6\9) // example vector
          mata: st_matrix("M", invvech(st_matrix("v")))
          mat list v
          matrix list M


          Thank you Mike, I have no knowledge of Mata, but the solution seems to work as well when I tried it. This command seems to create the lower triangular matrix well. Thank you for your reply!

          Comment


          • #6
            When you said you wanted a "lower triangular matrix", I took that to mean you wanted missing values in the upper half. So when you try to add M+M' you do indeed get nothing but twice the diagonal, because anything + missing values is missing value. In any case, it is easy enough to fill in the upper half:

            Code:
            forvalues j = 1/421 { // LOOP OVER COLUMNS
                forvalues i = 1/`=`j'-1' {    // LOOP OVER ROWS
                    matrix M[`i', `j'] = M[`j', `i']
                }
            }
            Note: If you subsequently -matrix list M-, Stata will recognize that the matrix is symmetric and it will not show the upper triangle in the listing. But the numbers are there, as you can verify, if you wish, with commands like -display M[2, 3]-.

            Comment


            • #7
              Originally posted by Clyde Schechter View Post
              When you said you wanted a "lower triangular matrix", I took that to mean you wanted missing values in the upper half. So when you try to add M+M' you do indeed get nothing but twice the diagonal, because anything + missing values is missing value. In any case, it is easy enough to fill in the upper half:

              Code:
              forvalues j = 1/421 { // LOOP OVER COLUMNS
              forvalues i = 1/`=`j'-1' { // LOOP OVER ROWS
              matrix M[`i', `j'] = M[`j', `i']
              }
              }
              Note: If you subsequently -matrix list M-, Stata will recognize that the matrix is symmetric and it will not show the upper triangle in the listing. But the numbers are there, as you can verify, if you wish, with commands like -display M[2, 3]-.
              Thank you. I thought it would be easier to generate the lower half as the data was from the lower half and hence at first only asked for the lower half but your code works perfectly to generate the full symmetrical matrix. You are a lifesaver!

              Comment

              Working...
              X