I try to understand a small simulation code. However, I just confused about the result from runif
function stored in an array
.
Here is the code:
N=10
dim=3
v = array(NA, c(dim, dim))
x <- array(NA, dim=c(N, dim))
for(s in 1:N)
{
w <- runif(dim)
x[s,1] <- w[1]
v[1,1] <- w[1]
}
x
w
will be 3 uniform numbers. x[s,1]
is the first column of x
array. x[s,1] <- w[1]
that means all the values in the first column must take the first value of the vector w
. However, when I print x
I got 10
different values. It is really confused me. When I try to use x[1,1] <- w[1]
, it works just fine! However, x[s,1] <-w[1]
is just x[,1] <- runif(N)
see the comments of @zx8754.
Do I miss something? How does this code work? Any help, please?
Aucun commentaire:
Enregistrer un commentaire