Soberon Velez, Alexandra Pilar
2011-Aug-25 01:10 UTC
[R] How to store the output of a loop into a matrix??
Hello, I want to create a matrix of N random numbers with a uniform distributions. Later, I want to repeat T times each row of this matrix. For this I do the following loop: N<-45 T<-10 n<-N*T a<-matrix(runif(N,min=-1,max=1),nr=N) mymat<-matrix(rep(NA,n),nr=n,nc=1) for(i in i:N){ b<-rep(a[i,],T) mymat[i,]<-b } Mi problem is that with this loop I can see the output of the loop but I canot get the matrix (mymat) that contains the full output of the loop. Please, somebody know how can I create a matrix that contains the output of the loop with a dimension NTx1 (450x1). Thanks a lot for your help. Alexandra [[alternative HTML version deleted]]
Hi Alexandra, Here is an alternative without using a loop: matrix(sapply(a, rep, T), ncol = 1) HTH, Jorge On Wed, Aug 24, 2011 at 9:10 PM, Soberon Velez, Alexandra Pilar <> wrote:> Hello, > > I want to create a matrix of N random numbers with a uniform distributions. > Later, I want to repeat T times each row of this matrix. For this I do the > following loop: > > N<-45 > T<-10 > n<-N*T > a<-matrix(runif(N,min=-1,max=1),nr=N) > > mymat<-matrix(rep(NA,n),nr=n,nc=1) > for(i in i:N){ > b<-rep(a[i,],T) > mymat[i,]<-b > } > > Mi problem is that with this loop I can see the output of the loop but I > canot get the matrix (mymat) that contains the full output of the loop. > > Please, somebody know how can I create a matrix that contains the output of > the loop with a dimension NTx1 (450x1). > > Thanks a lot for your help. > Alexandra > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hi: It's straightforward to write a simple function to do this: simmat <- function(N, t1) matrix(rep(runif(N, min = -1, max = 1), each = t1), nrow = N, byrow = TRUE) v <- simmat(45, 10) head(v, 3) HTH, Dennis PS: T is a reserved word in R (abbreviation for the logical TRUE) and should not be used as a variable name; similarly, t is the name of a very common function (for matrix transpose) so it shouldn't be used as a variable name, either. This is why I chose t1 in the function above. On Wed, Aug 24, 2011 at 6:10 PM, Soberon Velez, Alexandra Pilar <alexandra.soberon at unican.es> wrote:> Hello, > > I want to create a matrix of N random numbers with a uniform distributions. Later, I want to repeat T times each row of this matrix. For this I do the following loop: > > N<-45 > T<-10 > n<-N*T > a<-matrix(runif(N,min=-1,max=1),nr=N) > > mymat<-matrix(rep(NA,n),nr=n,nc=1) > for(i in i:N){ > b<-rep(a[i,],T) > mymat[i,]<-b > } > > Mi problem is that with this loop I can see the output of the loop but I canot get the matrix (mymat) that contains the full output of the loop. > > Please, somebody know how can I create a matrix that contains the output of the loop with a dimension NTx1 (450x1). > > Thanks a lot for your help. > Alexandra > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >