Re: style question
- To: mathgroup at smc.vnet.net
- Subject: [mg79535] Re: style question
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Sat, 28 Jul 2007 05:45:00 -0400 (EDT)
On 7/26/07 at 5:39 AM, carlw at wolfram.com (Carl Woll) wrote: >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[5]:= Count[Total[Unitize[m1 - m2], {2, 3}], 0] // Timing >Out[5]= {0.391, 62300} On my machine, it is somewhat faster to make use this idea and SparseArray, i.e., In[15]:= k = 10^6; m1 = RandomInteger[1, {k, 2, 2}]; m2 = RandomInteger[1, {k, 2, 2}]; In[18]:= Count[Total[Unitize[m1 - m2], {2, 3}], 0] // Timing Out[18]= {1.12376,62590} In[19]:= k - (SparseArray[Total[Unitize[m1 - m2], {2, 3}]] /. SparseArray[_, _, _, a_] :> Length[Last@a]) // Timing Out[19]= {0.929573,62590} In[20]:= $Version Out[20]= 6.0 for Mac OS X PowerPC (32-bit) (June 19, 2007) -- To reply via email subtract one hundred and four
- Follow-Ups:
- Re: Re: style question
- From: Carl Woll <carlw@wolfram.com>
- Re: Re: style question