R for loop in matrix -
i have 2 365x1 matrix s1 , s2, mean , standard deviation vector. want simulate normal distribution new 365x1 matrix. code used is
sim<-matrix(rep(na,365),nrow=365,ncol=1) (i in 1:365){y<-rnorm(1,s1[i,],s2[i,]) sim[i,]<-y[i]}
however generates first value. how should fix codes? thank much!
no need loop. rnorm vectorised this...
s1 <- sample(10,365,repl=true) s2 <- sample(3,365,repl=true) rnorm( 365 , s1 , s2 ) #[1] 5.83648500 1.64208807 0.02800676 -1.76443571 5.15361880 2.88269571 . . .
this draw 365 random normal deviates using each of values in mean , standard deviation vectors s1
, s2
in turn.
Comments
Post a Comment