Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have two matrices, the first one is mat 2000*500, the second one is 6000*500 tep I did this code for some analysis to get another matrix that has a specific elements.

Problem is : This code is very very slow, I need to make faster.Any suggestion to do so is welcome

mat=matrix(sample(c(0,1,2),200,replace =T),nrow=500,ncol=2000)
tep =matrix(rnorm(200,5,2),nrow=500,ncol=6000)`

The code is trying to create one matrix (500 row by 2000 col). Its elements depend on the following : Let the new matrix is A .

if mat[1,1] = 0 then A[1,1]=tep[1,1]/(sum(tep[1,1:3]))
if mat[1,1] = 1 then A[1,1]=tep[1,2]/(sum(tep[1,1:3]))
if mat[1,1] = 2 then A[1,1]=tep[1,3]/(sum(tep[1,1:3]))
if mat[1,2] = 0 then A[1,2]=tep[1,4]/(sum(tep[1,4:6]))
if mat[1,2] = 1 then A[1,2]=tep[1,5]/(sum(tep[1,4:6])) 
if mat[1,2] = 2 then A[1,2]=tep[1,6]/(sum(tep[1,4:6]))`
...... 

and so on.. The one column in mat is corresponding to three columns in tep. Every three values in three columns in matrix tep at a j row is a group we calculate from each group there will be one value according to the example above, so we will end up with a 500 row by 2000 column matrix.

allele=c(0,1,2)
A=matrix(nrow=500,ncol=2000)
 start=seq(1,6000,3)
for(k in 1:500){
for(i in 1:2000){
for(j in start){
  temp.z= mat[k,i]
  temp.pl=tep[k,j:(j+2)]
  loc=which(allele==temp.z)
  temp.pl=temp.pl[loc]/(sum(temp.pl))
  A[k,i]=temp.pl
  rm(temp.pl,temp.z,loc)

      }
   }
}
share|improve this question
    
Your explanation is not very clear. Maybe you could rewrite it a bit. Also, the code at the end does not seem to modify A, which I think is what you are trying to compute. –  toto2 Oct 22 '14 at 0:06
    
i do some change thanks for your point –  Neal Oct 22 '14 at 0:27

1 Answer 1

up vote 2 down vote accepted

Try the following:

mat0 <- tep[, c(TRUE, FALSE, FALSE)]
mat1 <- tep[, c(FALSE, TRUE, FALSE)]
mat2 <- tep[, c(FALSE, FALSE, TRUE)]

denominator <- mat0 + mat1 + mat2
numerator   <- mat0 * (mat - 1) * (mat - 2) / (0 - 1) / (0 - 2) +
               mat1 * (mat - 0) * (mat - 2) / (1 - 0) / (1 - 2) +
               mat2 * (mat - 0) * (mat - 1) / (2 - 0) / (2 - 1)

A <- numerator / denominator
share|improve this answer
    
Amazing, thanks –  Neal Oct 22 '14 at 21:20
    
Now that's mathematical optimization! :) –  jackStinger Dec 2 '14 at 10:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.