[R] Newbie: colSums() compared with Matlab's sum()

Francesco Napolitano franapoli at gmail.com
Thu Dec 24 04:22:58 CET 2009


Il giorno mer, 23/12/2009 alle 22.14 -0500, David Winsemius ha scritto:
> > for(i in 1:n){
> >    submat <- data[1:i,]
> >    C <- colSums(submat)
> >    }
> >
> In R the loop is not necessary, even confusing as you are demonstrating:
> 
>  > mat <- matrix(rnorm(100), nrow=10)  # 10 x 10
>  > colSums(mat[1:5, ])                 # sums of 1st 5 rows
>   [1]  3.33331735  0.86672248 -3.10971483  1.23620455 -0.31887421  
> -0.50544837
>   [7]  3.13636155  0.02175862 -2.18816961 -1.31760196

It would be unnecessary also in Matlab. In the real code I need to store
the partial sums (the example overwrites them, so it looks like wasted
loops).

Francesco.




More information about the R-help mailing list