Announcement

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

  • Extracting elements from matrix

    Hi,
    Is there any way of extracting specific information from a matrix. For example:

    Code:
    sysuse auto, clear
    
    qui reg price mpg weight
    mat m =r(table)'
    
    mat li m
    
    m[3,9]
                     b          se           t      pvalue          ll          ul          df        crit       eform
       mpg  -49.512221   86.156039  -.57468079   .56732373  -221.30248   122.27804          71   1.9939434           0
    weight   1.7465592   .64135379   2.7232382   .00812981   .46773602   3.0253823          71   1.9939434           0
     _cons   1946.0687   3597.0496   .54101802   .59018863  -5226.2445   9118.3819          71   1.9939434           0
    
    mat myelement = m[1, 1 .. 3]
    mat li myelement
    
    myelement[1,3]
                  b          se           t
    mpg  -49.512221   86.156039  -.57468079
    Until now this is fine. But is there any way to extract only 1st, 3rd and 5th elements of first row rather using a range?

    Thanks.



    Roman

  • #2
    Code:
     mat list A
    
        c1  c2  c3
    r1   1   2   3
    r2   4   5   6
    
    . mat B = A[1,1],A[1,3]
    
    . mat list B
    
    B[1,2]
        c1  c2
    r1   1   3
    Steve Samuels
    Statistical Consulting
    [email protected]

    Stata 14.2

    Comment


    • #3
      Thanks Steve, very helpful.
      Roman

      Comment


      • #4
        You're welcome, Roman. You can also use column and row names with this method

        Code:
        . mat list A
        
        A[2,3]
               red   green  yellow
        r1       1       2       3
        r2       4       5       6
        
        . mat B= A[1,"red"],A[1,"yellow"]
        
        . mat list B
        
        B[1,2]
               red  yellow
        r1       1       3
        Steve Samuels
        Statistical Consulting
        [email protected]

        Stata 14.2

        Comment


        • #5
          Thanks again Steve. Another question, how to show the full row/column names and avoid truncation, such as the one below:

          Code:
          mat A = 1,2,3\4,5,6
          mat colnames A= redbrickhouse greengrasshouse yellowwallhouse
          mat rownames A= myhousenumers yourhousenumbers
          mat li A
          
          A[2,3]
                        redbrickho~e  greengrass~e  yellowwall~e
          myhousenum~s             1             2             3
          yourhousen~s             4             5             6
          Roman

          Comment


          • #6
            Try the matlist command.
            Steve Samuels
            Statistical Consulting
            [email protected]

            Stata 14.2

            Comment

            Working...
            X