Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*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 smc.vnet.net
  • Subject: [mg75478] Re: Logical comparisons of items in a two lists
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 2 May 2007 03:53:54 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f16q2u$7er$1@smc.vnet.net>

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".
> 
> Thanks
> 
> Larry
> 
> 

Hi Larry,

Since you show neither your code (or a similar example) nor any timing,
it is hard to tell where the bottleneck is located, although I would bet 
on the *Table* part.

In the code below, we compare two lists of 10,000,000 real numbers 
(machine precision) each. Comparing the pairs of elements with *Table* 
took nearly twice the time needed by *MapThread*. (Note that the 
difference is less dramatic for smaller values of n.) Moreover, you can 
see that the built-in function *Count* is highly efficient: it took less 
than 0.8 second to count all the *True* values.

In[1]:=
$HistoryLength=0;
n=7;
a=Table[Random[],{10^n}];
b=Table[Random[],{10^n}];
Timing[comp=Table[a[[i]]>b[[i]],{i,10^n}];]
Timing[comp=MapThread[#1>#2&,{a,b}];]
Timing[Count[comp,True]]

Out[5]=
{30.563 Second,Null}

Out[6]=
{18.766 Second,Null}

Out[7]=
{0.797 Second,4998689}

Of course, you may have done something completely different, by I could 
not read your mind!

Best regards,
Jean-Marc



  • Prev by Date: Re: Simplification
  • Next by Date: exceptional group symmetry breaking as a binary entropy process
  • Previous by thread: Re: Logical comparisons of items in a two lists
  • Next by thread: Re: Logical comparisons of items in a two lists