Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: Logical comparisons of items in a two lists

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

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 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.

$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]]]

{31.391 Second,4998967}

{19.656 Second,4998967}

{13.766 Second,4998967}

{9. Second,4998967}

{7.078 Second,4998967}


  • 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