This is the code: x<-matrix(rnorm(20),5) y<-list() for (i in seq(nrow(x))) y[[i]]<-t(x[i,,drop=F])%*%x[i,,drop=F] y[[1]]+y[[2]]+y[[3]]+y[[4]]+y[[5]] How can I do it without using for loops? Thank you in advance! -- ronggui Deparment of Sociology Fudan University
Hello, One solution is: lapply(1:nrow(x), function(i){ t(x[i,,drop=FALSE]) %*% x[i,,drop=FALSE] }) Best, Matthias> > This is the code: > > x<-matrix(rnorm(20),5) > y<-list() > for (i in seq(nrow(x))) y[[i]]<-t(x[i,,drop=F])%*%x[i,,drop=F] > y[[1]]+y[[2]]+y[[3]]+y[[4]]+y[[5]] > > How can I do it without using for loops? > Thank you in advance! > -- > ronggui > Deparment of Sociology > Fudan University > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read > the posting guide! http://www.R-project.org/posting-guide.html >
On Tue, 28 Feb 2006, ronggui wrote:> This is the code: > > x<-matrix(rnorm(20),5) > y<-list() > for (i in seq(nrow(x))) y[[i]]<-t(x[i,,drop=F])%*%x[i,,drop=F] > y[[1]]+y[[2]]+y[[3]]+y[[4]]+y[[5]] > > How can I do it without using for loops?crossprod(x) Uwe Ligges> Thank you in advance! > -- > ronggui > Deparment of Sociology > Fudan University > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Try: crossprod(x) or t(x) %*% x On 2/28/06, ronggui <ronggui.huang at gmail.com> wrote:> This is the code: > > x<-matrix(rnorm(20),5) > y<-list() > for (i in seq(nrow(x))) y[[i]]<-t(x[i,,drop=F])%*%x[i,,drop=F] > y[[1]]+y[[2]]+y[[3]]+y[[4]]+y[[5]] > > How can I do it without using for loops? > Thank you in advance! > -- > ronggui > Deparment of Sociology > Fudan University > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >