I am attempting to create a cumulative matrix from another matrix. I was playing around with loops and lappy. The following seems to work, but it's very slow. The actual matrix is much larger. I am wondering if there is a more efficient way to create the cumulatrive matrix. Thanks in advance.

```
# example original matrix:
mat1<-cbind(1:5,1:5,1:5)
# new matrix with the cumulative sum of all previous columns in mat1.
mat2<-mat1
for(i in 2:ncol(mat1)){
mat2[,i]=apply(mat1[,1:i],1,sum)
}
```