Re: [R] matrix loop
John Kane [EMAIL PROTECTED] wrote in news:[EMAIL PROTECTED]: You are trying to create a matrix in the loop Try creating the matrix before the loop m - 1:5 n-1:10 y - matrix(rep(NA, 50), nrow=m) # I think that this might actually work: y - matrix(rep(NA, 50), nrow=max(m)) for(i in 1:length(m)) { for(j in 1:length(n)) { y[i,j]=sum(i,j) } } #produced: Error in y[i, j] = sum(i, j) : subscript out of bounds -- David Winsemius However as Jim Holtman points out you can do this particular matrix by outer(1:5, 1:10, +) --- mohamed nur anisah [EMAIL PROTECTED] wrote: Dear list, I'm trying to make a loop of a (5x10) matrix and below are my codes. Could anybody help me figure out why my loop is not working. Thanks in advance!! m-1:5 n-1:10 for(i in 1:length(m)) { for(j in 1:length(n)) { y[i,j]=sum(i,j) y-as.matrix(y[i,j]) } } cheers, __ 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.
Re: [R] matrix loop
You are trying to create a matrix in the loop Try creating the matrix before the loop m - 1:5 n-1:10 y - matrix(rep(NA, 50), nrow=m) for(i in 1:length(m)) { for(j in 1:length(n)) { y[i,j]=sum(i,j) } } However as Jim Holtman points out you can do this particular matrix by outer(1:5, 1:10, +) --- mohamed nur anisah [EMAIL PROTECTED] wrote: Dear list, I'm trying to make a loop of a (5x10) matrix and below are my codes. Could anybody help me figure out why my loop is not working. Thanks in advance!! m-1:5 n-1:10 for(i in 1:length(m)) { for(j in 1:length(n)) { y[i,j]=sum(i,j) y-as.matrix(y[i,j]) } } cheers, Anisah - [[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. __ 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.
Re: [R] matrix loop
What exactly are you intending the loop to do? Why do you have the 'as.matrix' in the middle of the loop? Where was 'y' defined? Does this do what you want? outer(1:5, 1:10, +) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,]23456789 1011 [2,]3456789 10 1112 [3,]456789 10 11 1213 [4,]56789 10 11 12 1314 [5,]6789 10 11 12 13 1415 On Feb 6, 2008 7:52 PM, mohamed nur anisah [EMAIL PROTECTED] wrote: Dear list, I'm trying to make a loop of a (5x10) matrix and below are my codes. Could anybody help me figure out why my loop is not working. Thanks in advance!! m-1:5 n-1:10 for(i in 1:length(m)) { for(j in 1:length(n)) { y[i,j]=sum(i,j) y-as.matrix(y[i,j]) } } cheers, Anisah - [[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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ 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.