MathGroup Archive 2007

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

Search the Archive

Re: Logical comparisons of items in a two lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75484] Re: Logical comparisons of items in a two lists
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 2 May 2007 03:57:04 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f16q2u$7er$1@smc.vnet.net> <f1720h$9kj$1@smc.vnet.net>

dh wrote:
> Hi Larry,
> 
> what's wrong with Count? It is pretty efficient. However, you could 
> 
> speed up the creation of the list a bit by e.g.: Thread[list1 > list2] 
> 
> or you could use Compilation:
> 
> fun=Compile[{{v1,_Real,1},{v2,_Real,1}},Table[Boole[v1[[i]]<v2[[i]]],{i,1,Length[v1]}]];
 >
> Total@ fun[list1,list2]
> 
> hope this helps, Daniel
> 
> 
> 
> actuary at mchsi.com wrote:
> 
>> Hello:
> 
> 
>> I have two lists of real numbers, a & b.  I want two compare
> 
>> individual items in one list to the corresponding items in the other
> 
>> list.  For example Is a[[1]] > b[[1]]. At the end of the comparisons,
> 
>> I want to count the "Trues".  I know how to do this use a "Table"
> 
>> statement and a "Count" statement.  Is there a quicker, more efficient
> 
>> way of counting the number of "Trues".

I read Daniel's reply after I had sent my comparison tests. In the 
updated tests (see below), you will notice that Daniel's solutions are 
faster than mine; that is Table is slower than MapThread which is slower 
than Thread which is slower than the compile function using Boole and Total.

In[1]:=
$HistoryLength = 0;
n = 7;
a = Table[Random[], {10^n}];
b = Table[Random[], {10^n}];
Timing[Count[Table[a[[i]] > b[[i]], {i, 10^n}], True]]
Timing[Count[MapThread[#1 > #2 & , {a, b}], True]]
Timing[Count[Thread[a > b], True]]
fun = Compile[{{v1, _Real, 1}, {v2, _Real, 1}},
     Table[Boole[v1[[i]] > v2[[i]]],
      {i, 1, Length[v1]}]];
Timing[Count[fun[a, b], 1]]
Timing[Total[fun[a, b]]]

Out[5]=
{31.391 Second,4998967}

Out[6]=
{19.656 Second,4998967}

Out[7]=
{13.766 Second,4998967}

Out[9]=
{9. Second,4998967}

Out[10]=
{7.078 Second,4998967}

Cheers,
Jean-Marc


  • Prev by Date: exceptional group symmetry breaking as a binary entropy process
  • Next by Date: Re: Fourier and InverseFourier
  • Previous by thread: Re: Logical comparisons of items in a two lists
  • Next by thread: Re: associate a parameter name with a string