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)
}
}
}
A
, which I think is what you are trying to compute. \$\endgroup\$