I am not sure what you mean by being the same each time. If I make successive calls to the function, I get different results:
> z [,1] [1,] 0.0000 [2,] 0.0000 [3,] 201.6382 [4,] 0.0000 [5,] 0.0000 [6,] 0.0000 [7,] 0.0000 > matmult(InitialPop,1) [,1] [1,] 0.000 [2,] 0.000 [3,] 130.998 [4,] 0.000 [5,] 0.000 [6,] 0.000 [7,] 0.000 > matmult(InitialPop,1) [,1] [1,] 0.0000 [2,] 0.0000 [3,] 219.5943 [4,] 0.0000 [5,] 0.0000 [6,] 0.0000 [7,] 0.0000 > matmult(InitialPop,1) [,1] [1,] 0.0000 [2,] 0.0000 [3,] 200.0077 [4,] 0.0000 [5,] 0.0000 [6,] 0.0000 [7,] 0.0000 > matmult(InitialPop,1) [,1] [1,] 0.0000 [2,] 0.0000 [3,] 256.1736 [4,] 0.0000 [5,] 0.0000 [6,] 0.0000 [7,] 0.0000 On Wed, Sep 9, 2009 at 9:36 AM, RFish<tomworthing...@talk21.com> wrote: > > 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. > > ______________________________________________ > R-help@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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ R-help@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.