Skip to content
Prev 245779 / 398506 Next

how to replace my double for loop which is little efficient!

Dear all,

My double for loop as follows, but it is little efficient, I hope all
friends can give me a "vectorized" program to replace my code. thanks

x: is a matrix  202*263,  that is 202 samples, and 263 independent variables

num.compd<-nrow(x); # number of compounds
diss.all<-0
for( i in 1:num.compd)
   for (j in 1:num.compd)
      if (i!=j) {
        S1<-sum(x[i,]*x[j,])
        S2<-sum(x[i,]^2)
        S3<-sum(x[j,]^2)
        sim2<-S1/(S2+S3-S1)
        diss2<-1-sim2
        diss.all<-diss.all+diss2}

it will cost a long time to finish this computation! i really need "rapid"
code to replace my code.

thanks

kevin