MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Speeding up Vector Comparison

  • To: mathgroup at smc.vnet.net
  • Subject: [mg61414] Speeding up Vector Comparison
  • From: Lee Newman <leenewm at umich.edu>
  • Date: Tue, 18 Oct 2005 02:45:16 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Group,

I have a compiled function that compares two vectors by computing the 
mean of the absolute value of the difference between the two vectors -- 
subject to the constraint that if both sub-elements of the vector are 
less then a threshold value, they do not contribute to the difference 
(see example below).  The function gets executed about 100million times 
over the course of a simulation.  I've optimized it as much as I can, 
but it is still a bottleneck (~7 hrs).  Can anyone see an obvious way to 
rewrite my function (below) so that it runs faster?

Example (the actual vectors I am using have length=100):
v1 = {0.9, 0.9, 0.1, 0.1};
v2 = {0.5, 0.1, 0.5, 0.1};
threshold=0.5
for these two vectors, I want to compute the mean of the absolute value 
of the difference between the two vectors, ignoring the last position 
because the last value in *both* vectors is < threshold.

My Function

MeanDiff = Compile[{{vector1, _Real,1}, {vector2, _Real, 1}, {threshold, 
_Real}},
       Mean@ Abs[Plus @@@ Select[Transpose[{vector1, -vector2}], Max[Abs 
/@ #] > threshold &]]
       , {{Mean[_], _Real}}];

Note:  I use Plus rather than Subtract because Mathematica doesn't allow 
Subtract with @@@ in compiled functions.  The third argument to Compile 
seems to be necessary, as without it Mathematica complains that the function 
doesn't return a tensor of size 1.

Thanks for any suggestions,
Lee Newman


  • Prev by Date: NestList
  • Next by Date: Re: Re: Element test
  • Previous by thread: Re: NestList
  • Next by thread: Re: Speeding up Vector Comparison