Indeed! Your example works perfectly.
I think I was using the for-loop to try to address cases where thesize of the 
matrix was unknown. But your suggestion works forarbitrary sizes, just have to 
know the range of values to rep (repeat) and have"each" and "ncol" equal.
Thank you.ChrisMy apologies to Dennis for direct reply, I'm not used to using 
'reply all': tales of havoc wrought
----------------------------------------
> Date: Tue, 10 May 2011 11:19:51 -0700
> Subject: Re: [R] for loop populate matrix
> From: djmu...@gmail.com
> To: sgl...@hotmail.com
> CC: r-help@r-project.org
>
> Why loop?
>
> matrix(rep(3:0, each = 5), ncol = 5, byrow = TRUE)
> [,1] [,2] [,3] [,4] [,5]
> [1,] 3 3 3 3 3
> [2,] 2 2 2 2 2
> [3,] 1 1 1 1 1
> [4,] 0 0 0 0 0
>
> HTH,
> Dennis
>
> On Tue, May 10, 2011 at 11:10 AM, Chris English  wrote:
> >
> > Dear List,
> > I am trying to populate a matrix using a for loop.This works.
> >> four<- matrix(nrow=4,ncol=5)> for (j in 1:ncol(four)){+ for (i in 
> >> 1:nrow(four)){+ four[i,j]<-i-1}+ }> print(four)á á á[,1] [,2] [,3] [,4] 
> >> [,5][1,] á á0 á á0 á á0 á á0 á á0[2,] á á1 á á1 á á1 á á1 á á1[3,] á á2 á 
> >> á2 á á2 á á2 á á2[4,] á á3 á á3 á á3 á á3 á á3
> > What I would like is the inverse ofthe above, i.e.: á á [,1] [,2] [,3] [,4] 
> > [,5][1,] á á 3 á á3 á á3 á á3 á á3[2,] á á 2 á á2 á á2 á á2 á á2[3,] á á 1 
> > á á1 á á1 á á1 á á1[4,] á á 0 á á0 á á0 á á0 á á0
> > and can't seem to get there. áThanks,Chris
> > ______________________________________________
> > 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.

Reply via email to