[R] for loop populate matrix

From: Chris English <sglish_at_hotmail.com>
Date: Tue, 10 May 2011 14:10:10 -0400

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_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. Received on Tue 10 May 2011 - 18:16:39 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 10 May 2011 - 18:40:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive