Announcement

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

  • running/cumulative sum along all columns of a matrix

    I know this can be done in a loop, but am curious if (i) I missed some built-in way of doing this or (ii) am doing it inefficiently somehow. Here are two loop approaches that give the desired output:

    Code:
    x = (150, 20, 10 \ 10, 20, 30 \ 30, 10, 10 )
    
    nr = rows(x)
    nc = cols(x)
    
    xcol_rsum = J(nr,nc,0)
    
    rsumi = J(1,nc,0)
    for (i=1;i<=nr;i++){
        rsumi = rsumi + x[i,]
        xcol_rsum[i,] = rsumi
    }
    
    // alternately
    
    xcol_rsum2 = J(nr,nc,0)
    for (j=1;j<=nc;j++) xcol_rsum2[,j] = runningsum(x[,j])
Working...
X