Skip to content
Prev 166935 / 398502 Next

Vectorization of three embedded loops

Hello,

I believe that your bottleneck lies at this piece of code:

sum<-c();
for(j in 1:length(val)){
	sum[j]<-euc[rownames(start.b)[i],val[j]]
}

In order to speed up your code, there are two alternatives:

1) Try to reorder the euc matrix so that the sum vector corresponds to
(part of) a row or column of euc.

2) For each i value, create a matrix with the coordinates corresponding
to ( rownames(start.b)[i], val[j] ) and index the matrix by this matrix
in order to create sum. This will be easiest if you can reorder euc in a
way that accessing its elements will be easy (and then you would be back
into (1)).

Creating a variable sum as c() and increasing its size in a loop is one
of the easiest ways to uselessly burn your CPU.

Best regards,

Carlos J. Gil Bellosta
http://www.datanalytics.com
On Wed, 2009-01-14 at 10:32 +0300, Thomas Terhoeven-Urselmans wrote: