Amarjit Singh Sethi
2011-Apr-09 03:22 UTC
[R] How to define a number of matrices through loops
Hi all I need to deal with a number (say, 5) of diferent ordered matrices? simultaneously?in my computational work. I tried to define these matrices through looping, but got an error message: row <- c(3,4,6,4,5) mt <- c() for (i in 1:5){? mt[i] <- matrix(nrow=row[i],ncol=4) } mt Kindly help Yours ajss
Hi Ajss, You instantiated mt as a vector, not a list. Try: row <- c(3,4,6,4,5) mt <- vector("list", length(row)) # list with length of "row" elements for (i in 1:5){ mt[[i]] <- matrix(nrow=row[i], ncol=4) } mt Or in one line: mt <- lapply(1:5, function(x) matrix(nrow = x, ncol = 4)) mt Hope this helps, Josh On Fri, Apr 8, 2011 at 8:22 PM, Amarjit Singh Sethi <set_alt at yahoo.co.in> wrote:> Hi all > I need to deal with a number (say, 5) of diferent ordered matrices > simultaneously?in my computational work. I tried to define these matrices > through looping, but got an error message: > > row <- c(3,4,6,4,5) > mt <- c() > for (i in 1:5){ > mt[i] <- matrix(nrow=row[i],ncol=4) > } > mt > > Kindly help > > Yours > ajss > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/