Re: Speeding up Vector Comparison
- To: mathgroup at smc.vnet.net
- Subject: [mg61441] Re: [mg61414] Speeding up Vector Comparison
- From: Pratik Desai <pdesai1 at umbc.edu>
- Date: Wed, 19 Oct 2005 02:16:31 -0400 (EDT)
- References: <200510180645.CAA11280@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Lee Newman wrote:
>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
>
>
>
Hi
Here is my attempt using the Matrix Manipulation package (most likely
not what you are looking for, but hopefully trigers another probably
faster approach).
<< LinearAlgebra`MatrixManipulation`
v1 = {0.9, 0.9, 0.1, 0.1};
v2 = {0.5, 0.1, 0.5, 0.1};
thresh = 0.5
vnew = {v1, v2}
(* Here I find row position where the row has atleast one member >
threshold value*)
pos = Position[
Table[If[Max[Flatten[
Table[TakeRows[Transpose[vnew], {j, j}], {j, 1,
Length[v1]}][[j]]]] > thresh,
1, 0], {j, 1, 4}], 1]
(* Probably better way to do this subtraction*)
v11 = TakeColumns[Extract[Transpose[vnew], pos], 1]//Flatten
v22 = TakeColumns[Extract[Transpose[vnew], pos], -1]//Flatten
Mean[v11 - v22]
>>0.6
Hope this helps
Pratik .
--
Pratik Desai
Graduate Student
UMBC
Department of Mechanical Engineering
Phone: 410 455 8134
- References:
- Speeding up Vector Comparison
- From: Lee Newman <leenewm@umich.edu>
- Speeding up Vector Comparison