Re: style question
- To: mathgroup at smc.vnet.net
- Subject: [mg79428] Re: [mg79315] style question
- From: Carl Woll <carlw at wolfram.com>
- Date: Thu, 26 Jul 2007 05:39:32 -0400 (EDT)
- References: <200707241008.GAA26147@smc.vnet.net>
Yaroslav Bulatov wrote:
>What is the recommended way of counting the number of matches in two
>lists?
>
>The natural way would be to thread over Equal, but Equal will evaluate
>before Thread gets to it. The method below works, but somehow feels
>wrong
>
>m1 = RandomInteger[{1}, {10^5, 2, 2}];
>m2 = RandomInteger[{1}, {10^5, 2, 2}];
>matches = Thread[temporary[m1, m2]] /. temporary -> Equal;
>Count[matches, True]
>
>
>
For this particular problem (in version 6), it's faster to use
arithmetic and total:
In[1]:= k = 10^6;
m1 = RandomInteger[1, {k, 2, 2}];
m2 = RandomInteger[1, {k, 2, 2}];
In[4]:= Timing[
matches = Thread[temporary[m1, m2]] /. temporary -> Equal;
Count[matches, True]]
Out[4]= {3.453, 62300}
In[5]:= Count[Total[Unitize[m1 - m2], {2, 3}], 0] // Timing
Out[5]= {0.391, 62300}
Carl Woll
Wolfram Research
- References:
- style question
- From: Yaroslav Bulatov <yaroslavvb@gmail.com>
- style question