[R] for loop populate matrix

Chris English sglish at hotmail.com
Tue May 10 20:10:10 CEST 2011


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 		 	   		  


More information about the R-help mailing list