[R] Matrices with randomly generated entries

David Winsemius dwinsemius at comcast.net
Wed Jan 20 23:05:27 CET 2010


On Jan 20, 2010, at 4:55 PM, jshort wrote:

>
> I'm attempting to generate matrices where the entries are randomly  
> generated
> numbers of specified distribution.
>
> The following code was an attempt to create a 3 by 3 matrix, where my
> entries where randomly generated from a uniform (0,1) distribution.
>
>
> x = matrix(0,ncol = 3, byrow = T)

 > x = matrix(0,ncol = 3, byrow = T)
 > x
      [,1] [,2] [,3]
[1,]    0    0    0

> for(i in 1:3) {
> for(j in 1:3) { x[i,j]= runif(1,0,1) }
> }
>
> The problem I get with the above code however is that I get an error  
> message
> which says the following;
>
> "Error in x[i,j]= runif(1,0,1)  :   subscript out of bounds"
>
> I haven't been able to figure out how to fix this.

Make your matrix large enough to accept the range of values for the  
indices.

> Any help would be much
> appreciated.
>
> jshort
> -- 
> View this message in context: http://n4.nabble.com/Matrices-with-randomly-generated-entries-tp1018777p1018777.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



More information about the R-help mailing list