[R] Matrix multiplication and random numbers
RFish
tomworthington at talk21.com
Wed Sep 9 15:36:32 CEST 2009
Dear All
I new to using R and am struggling with some matrix multiplication.
I have two matrices, one containing random numbers, these are multiplied
together to get another matrix which is different each time. When I put in
another for loop to repeat this process a multiple times the matrices are
all the same. I’m sure there is a way to keep the randomness of the
different matrices but I think I’m putting the for loop in the wrong place.
Also when I try and save the matrices using write.table only the first one
is saved
The code so far is below, any help would be greatly appreciated
Cheers
Tom
InitialPop<-matrix(c(500,0,0,0,0,0,0))
matmult<-function(InitialPop,N){
mat3<-matrix(c(0,rnorm(1,0.6021,0.0987),0,0,0,0,0,0,0,rnorm(1,0.6021,0.0987),0,0,0,0,1.9,0,0,rnorm(1,0.6021,0.0987),0,0,0,4.8,0,0,0,rnorm(1,0.6021,0.0987),0,0,9.7,0,0,0,0,rnorm(1,0.6021,0.0987),0,18,0,0,0,0,0,rnorm(1,0.6021,0.0987),32.6,0,0,0,0,0,0),nrow=7)
for (i in 1:N){
PVAmatrix<-matrix(c(0,rnorm(1,0.6021,0.0987),0,0,0,0,0,0,0,rnorm(1,0.6021,0.0987),0,0,0,0,1.9,0,0,rnorm(1,0.6021,0.0987),0,0,0,4.8,0,0,0,rnorm(1,0.6021,0.0987),0,0,9.7,0,0,0,0,rnorm(1,0.6021,0.0987),0,18,0,0,0,0,0,rnorm(1,0.6021,0.0987),32.6,0,0,0,0,0,0),nrow=7)
mat3<-mat3%*%PVAmatrix
}
ans<-mat3 %*% InitialPop
return(ans)
}
z<-matmult(InitialPop,1)
for (i in 1:4) print(z[,1])
write.table((z),"c:\\Documents and Settings\\...........................)
--
View this message in context: http://www.nabble.com/Matrix-multiplication-and-random-numbers-tp25365184p25365184.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list