Announcement

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

  • st_matrix for a matrix of strings

    Consider the following matrix of strings.
    Code:
    . mata: A = ("one", "two")
    I sometimes use the st_matrix command to make a Stata matrix from a Mata matrix like below:

    Code:
    . mata: B = (1, 2)
    . mata: st_matrix("B", B)
    . matrix list B
    However, according to the manual of Mata, st_matrix works only for a real matrix.

    Code:
    . mata: st_matrix("A", A)
                 st_matrix():  3253  nonreal found where real required
                     <istmt>:     -  function returned error
    So, I have checked whether the st_local command is available, but, you know, the st_local command is for a scalar.

    How can I make a Stata matrix from a string matrix from Mata?

  • #2
    Stata matrices only contain real numbers, so the answer to your question is you cannot. Are you simply trying to import a matrix of strings from Mata into a Stata dataset?

    Comment


    • #3
      You cant
      Stata matrix objects cannot contain strings

      Comment


      • #4
        Andrew Musau In fact, I want to specify the column names of a Stata matrix, and because the creation of the vector of the names is more convenient in Mata for me, I first generate the vector of names in Mata and then try to import the vector from Mata into Stata.

        Comment


        • #5
          Hi Minch
          I think you can work with "st_matrixcolstripe" with the following syntax

          st_matrixcolstripe("_bb", onames)

          where _bb is the name of the matrix in Stata, and oname is a 2 col n row string matrix with the matrix col names and col equation.
          F

          Comment


          • #6
            Originally posted by Minch Park View Post
            I want to specify the column names of a Stata matrix, and because the creation of the vector of the names is more convenient in Mata for me, I first generate the vector of names in Mata and then try to import the vector from Mata into Stata.
            As Fernando points out, look at the function -st_matrixcolstripe()- or just import the string matrix into Stata and define a loop that will concatenate the column and row names and then assign these to the matrix. See below:

            Code:
            clear
            set obs 5
            gen str29 rownames=""
            gen str29 colnames=""
            mata
            b1= ("first_r","second_r","third_r","fourth_r","fifth_r")',("first_c","second_c","third_c","fourth_c","fifth_c")'
            b1
            rownames=b1[1...,1]
            colnames=b1[1...,2]
            st_sstore(., "rownames", rownames)
            st_sstore(., "colnames", colnames)
            end
            
            matrix J= J(5, 5, .5)
            local colnames
            local rownames
            forval i= 1/`=colsof(J)'{
                local colnames "`colnames' `=colnames[`i']'"
                local rownames "`rownames' `=rownames[`i']'"
            }
            mat colnames J= `colnames'
            mat rownames J= `rownames'
            
            mat l J
            Res.:

            Code:
            . mata
            ------------------------------------------------- mata (type end to exit) --------------------------------------------------------------------------------------------------------------------------------------
            :
            : b1= ("first_r","second_r","third_r","fourth_r","fifth_r")',("first_c","second_c","third_c","fourth_c","fifth_c")'
            
            :
            : b1
                          1          2
                +-----------------------+
              1 |   first_r    first_c  |
              2 |  second_r   second_c  |
              3 |   third_r    third_c  |
              4 |  fourth_r   fourth_c  |
              5 |   fifth_r    fifth_c  |
                +-----------------------+
            
            :
            : rownames=b1[1...,1]
            
            :
            : colnames=b1[1...,2]
            
            :
            : st_sstore(., "rownames", rownames)
            
            :
            : st_sstore(., "colnames", colnames)
            
            :
            : end
            ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            
            .
            .
            .
            . matrix J= J(5, 5, .5)
            
            .
            . local colnames
            
            .
            . local rownames
            
            .
            . forval i= 1/`=colsof(J)'{
              2.
            .     local colnames "`colnames' `=colnames[`i']'"
              3.
            .     local rownames "`rownames' `=rownames[`i']'"
              4.
            . }
            
            .
            . mat colnames J= `colnames'
            
            .
            . mat rownames J= `rownames'
            
            .
            .
            .
            . mat l J
            
            symmetric J[5,5]
                       first_c  second_c   third_c  fourth_c   fifth_c
             first_r        .5
            second_r        .5        .5
             third_r        .5        .5        .5
            fourth_r        .5        .5        .5        .5
             fifth_r        .5        .5        .5        .5        .5
            
            .

            Comment


            • #7
              FernandoRios Andrew Musau The solution you have provided is what I was looking for exactly! Thank you so much!

              Comment

              Working...
              X