Hi all,
I have the following problem:
I have a vector with 40,000 observation and I am trying to find out
whether it contains any sets of two (or more) equal elements. To that end,
I have tried:
1) nested loops along the following lines:
j = rows(vLambda);
for(i = 0; i < j; ++i)
{
iX = vLambda[i];
for(k = i; k < j-1; ++k)
{
iY = vLambda[k+1];
if(iX == iY)
mOptimalLambda |= (vLambda[i] ~ vLambda[k+1]);
}
}
2) first transforming my vector into a matrix, the columns of which
consist of the differences between my vector and every successive element
of it so that equal values in the vector would show up as zeros in any
column, and then using logical statements to identify the zeros:
j = rows(vLambda);
decl mX = new matrix[j][0];
decl mY = new matrix[0][0];
for(i = 0; i < j; ++i)
{
mX ~= (vLambda[] - vLambda[i]);
}
f = columns(mX);
for(e = 0; e < f; ++e)
{
if(rows( vecindex(mX[][e] .== 0)) >= 2)
mY ~= vecindex(mX[][e] .== 0);
The problem is that both versions take ages to run on my machine (Pentium
I, 166MHz). My time projections point to hours if not days!
Does anyone happen to know of a more time efficient way to compare the
elements of a vector? Any comments would be gratefully received. Many
thanks.
Michael
________________________________
Michael Massmann
Lincoln College
Oxford OX1 3DR
England
tel: +44 (0)1865 271085 (office)
+44 (0)1865 246308 (home)
fax: +44 (0)1865 271094
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|