Announcement

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

  • Using matsum to sum values in a matrix row

    Hi,

    Using the following command I have the following matrix :
    Code:
    matrix list r(mad)
    Click image for larger version

Name:	Matrix.png
Views:	1
Size:	10.2 KB
ID:	1751862

    And want to sum the 22 values contained in r1. I tried the following command :
    Code:
    matsum r(mad), row(r1)
    But it didn't work/doesn't display anything. Any advice?

  • #2
    You can use the Mata function -rowsum()- to calculate row sums.

    Code:
    mat X= [1, 2, 3, 4, 5]
    mata: rowsum(st_matrix("X"))
    Res,:

    Code:
    . mat X= [1, 2, 3, 4, 5]
    
    . 
    . mata: rowsum(st_matrix("X"))
      15

    Comment


    • #3
      matsum is a community-contributed command from 1997:

      Code:
      . type http://www.stata.com/stb/stb39/dm49/matsum.ado
      *! 1.0.4  18Jun1997  Jeroen Weesie/ICS  STB-39 dm49
      
      program define matsum
              version 5.0
      
            * scratch 
      
              tempname A OneC OneR
              
            * parse input
              
              if "`*'" == "" { exit } 
              parse "`*'", parse(",")
              local A0 "`1'"
              capt mat `A' = `A0'
              if _rc {
                  di in red "`A0' is undefined or an illegal matrix expression"
                  exit 198
              }
              local nr = rowsof(`A')
              local nc = colsof(`A')
      
              mac shift
              local options " All(str) Column(str) Display Format(str) Row(str)"
              parse "`*'"
      
              if "`format'" != "" {
                  local fmt "format(`format')"
              }
              
            * defaults
       
              if "`row'"=="" & "`column'"=="" & "`all'"=="" {
                  local column "."
              }
              if "`row'"=="." { 
                  tempname Row
                  local row "`Row'"
                  local display "display"
              }
              if "`column'"=="." { 
                  tempname Col
                  local column "`Col'"
                  local display "display"
              }
              if "`all'"=="." { 
                  tempname All
                  local all "`All'"
                  local display "display"
              }
              
              mat `OneC' = J(`nc',1,1)
              mat `OneR' = J(1,`nr',1)
              
              if "`row'" != "" {            * compute row sums as a column vector
      
                  mat `row' = `A' * `OneC'
                  if "`display'" != "" { 
                      di in gr _n "row-wise sums of matrix `A0'[`nr',`nc']"
                      mat list `row', noheader `fmt' 
                  }
              }
      
              if "`column'" != "" {         * compute column sums as a row vector
      
                  mat `column' = `OneR' * `A' 
                  if "`display'" != "" { 
                      di in gr _n "column-wise sums of matrix `A0'[`nr',`nc']"
                      mat list `column', noheader `fmt' 
                  }
              }
      
              if "`all'" != "" {            * sum over all elements
      
                  if "`row'" != "" { 
                      mat `all' = `OneR' * `row' 
                  }
                  else if "`column'" != "" { 
                      mat = `all' * `OneC' 
                  }
                  else {
                      mat `all' = `A' * `OneC'
                      mat `all' = `OneR' * `all'
                  }
                  if "`display'" != "" { 
                      if "`format'" == "" { 
                          local format "%10.2g" 
                      }
                      di _n in gr "sum of elements of `A0'[`nr',`nc'] = " /*
                        */ in ye `format' `all'[1,1]
                  }
              }
      end
      Code:
      
      
      You don't say where the result r(mad) comes from, but my guess is that you need to put it in a matrix

      Code:
      matrix mad = r(mad) 
      before running matsum. It is a matrix but just not one that would have been recognised in Stata 5 in 1997.

      But I recommend using Mata directly. You don't need to install extras to get the sum of a matrix.

      Code:
      . matrix mad = (1,2,3)
      
      . mata : st_numscalar("sum", sum(st_matrix("mad")))
      
      . di scalar(sum)
      6
      There are other ways to do it, e.g. as an inner product:


      Code:
      . mat foo = J(1, 22, 1)
      
      . mat sum = J(1,22,1) * foo'
      
      . mat li sum
      
      symmetric sum[1,1]
          r1
      r1  22

      Comment


      • #4
        Thanks a lot for your answers! It really helps!
        Last edited by Marine Jouvin; 01 May 2024, 02:25.

        Comment

        Working...
        X