I would like to create a matrix in R that looks similar to this:
[,1] [,2] [,3] [,4]
[1,] NaN 1 2 3
[2,] NaN 1 2 4
[3,] NaN 1 2 5
[4,] NaN 2 3 4
[5,] NaN 2 3 5
[6,] NaN 3 4 5
I have the loop below:
where A for example is 5
matrixx<-function(A){
B=matrix(NaN,nrow=(A+1),ncol=4)
for(k in 1:(A+1)){
for(i in 1:(A-2)){
for(j in (i+2):A){
}
}
}
B[k,]=c(NaN,i,(i+1),j)
print(B)
}
But it only prints the final line in:
> matrixx(5)
[,1] [,2] [,3] [,4]
[1,] NaN NaN NaN NaN
[2,] NaN NaN NaN NaN
[3,] NaN NaN NaN NaN
[4,] NaN NaN NaN NaN
[5,] NaN NaN NaN NaN
[6,] NaN 3 4 5
Could anyone give me a hand? Would be much appreciated.
Thanks Emma
--
View this message in context:
http://www.nabble.com/numbers-loop-in-R-tp23099591p23099591.html
Sent from the R help mailing list archive at Nabble.com.
I would have expected to see the assignment to B[k,] inside the loops. And to see some connection with the k index in the inner loops if you did not want all of the rows to be similar. Because the assignment is outside the loops, it happens only once. -- David Winsemius On Apr 17, 2009, at 11:11 AM, emj83 wrote:> > I would like to create a matrix in R that looks similar to this: > > [,1] [,2] [,3] [,4] > [1,] NaN 1 2 3 > [2,] NaN 1 2 4 > [3,] NaN 1 2 5 > [4,] NaN 2 3 4 > [5,] NaN 2 3 5 > [6,] NaN 3 4 5 > > I have the loop below: > > where A for example is 5 > > matrixx<-function(A){ > B=matrix(NaN,nrow=(A+1),ncol=4) > for(k in 1:(A+1)){ > for(i in 1:(A-2)){ > for(j in (i+2):A){ > } > } > } > B[k,]=c(NaN,i,(i+1),j) > print(B) > } > > But it only prints the final line in: > >> matrixx(5) > [,1] [,2] [,3] [,4] > [1,] NaN NaN NaN NaN > [2,] NaN NaN NaN NaN > [3,] NaN NaN NaN NaN > [4,] NaN NaN NaN NaN > [5,] NaN NaN NaN NaN > [6,] NaN 3 4 5 > > Could anyone give me a hand? Would be much appreciated. > > Thanks Emma > > -- > View this message in context: http://www.nabble.com/numbers-loop-in-R-tp23099591p23099591.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
try this:> matrixx<-function(A){+ B=matrix(NaN,nrow=(A+1),ncol=4) + k <- 1 + for (i in 3:A){ + for (j in i:A) { + B[k,] <- c(NaN, i-2, i-1, j) + k <- k + 1 + } + } + B + }> matrixx(5)[,1] [,2] [,3] [,4] [1,] NaN 1 2 3 [2,] NaN 1 2 4 [3,] NaN 1 2 5 [4,] NaN 2 3 4 [5,] NaN 2 3 5 [6,] NaN 3 4 5>On Fri, Apr 17, 2009 at 11:11 AM, emj83 <stp08emj at shef.ac.uk> wrote:> > I would like to create a matrix in R that looks similar to this: > > ? ? [,1] [,2] [,3] [,4] > [1,] ?NaN ?1 ?2 ?3 > [2,] ?NaN ?1 ?2 ?4 > [3,] ?NaN ? 1 ?2 ?5 > [4,] ?NaN ?2 ?3 ?4 > [5,] ?NaN ?2 ?3 ?5 > [6,] ?NaN ? ?3 ? ?4 ? ?5 > > I have the loop below: > > where A for example is 5 > > matrixx<-function(A){ > B=matrix(NaN,nrow=(A+1),ncol=4) > ? ? ? ?for(k in 1:(A+1)){ > ? ? ? ? ? ? ? ?for(i in 1:(A-2)){ > ? ? ? ? ? ? ? ? ? ? for(j in (i+2):A){ > ? ? ? ? ? ? ? ? ? ? } > ? ? ? ? ? ? ? ?} > ? ? ? } > B[k,]=c(NaN,i,(i+1),j) > print(B) > } > > But it only prints the final line in: > >> matrixx(5) > ? ? [,1] [,2] [,3] [,4] > [1,] ?NaN ?NaN ?NaN ?NaN > [2,] ?NaN ?NaN ?NaN ?NaN > [3,] ?NaN ?NaN ?NaN ?NaN > [4,] ?NaN ?NaN ?NaN ?NaN > [5,] ?NaN ?NaN ?NaN ?NaN > [6,] ?NaN ? ?3 ? ?4 ? ?5 > > Could anyone give me a hand? Would be much appreciated. > > Thanks Emma > > -- > View this message in context: http://www.nabble.com/numbers-loop-in-R-tp23099591p23099591.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?