[R] Matrix multiplication and random numbers

RFish tomworthington at talk21.com
Wed Sep 9 16:51:23 CEST 2009


Sorry I probably wasn't clear with my description. The reason i put for loop
in was that I want to do the matrix multiplication about 1000 times to get a
1000 different matrices. Therefore I was hoping the for loop would be able
to automate this then use write.table to write to an external document

Cheers

Tom 

   

jholtman wrote:
> 
> 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<tomworthington at 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 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.
>>
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem that you are trying to solve?
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/Matrix-multiplication-and-random-numbers-tp25365184p25366609.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list