Re: Re: style question
- To: mathgroup at smc.vnet.net
- Subject: [mg79560] Re: [mg79535] Re: style question
- From: Carl Woll <carlw at wolfram.com>
- Date: Sun, 29 Jul 2007 00:13:36 -0400 (EDT)
- References: <200707280945.FAA00305@smc.vnet.net>
Bill Rowe wrote: >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 > > I used the Count approach for pedagogical reasons. It was already almost an order of magnitude faster than the OPs solution, and other solutions (that I could think of) were only marginally faster. At any rate, a slightly faster solution than your SparseArray approach is to use Total and Unitize again: k = 10^6; m1 = RandomInteger[1, {k, 2, 2}]; m2 = RandomInteger[1, {k, 2, 2}]; In[62]:= k - (SparseArray[Total[Unitize[m1 - m2], {2, 3}]] /. SparseArray[_, _, _, a_] :> Length[Last@a]) // Timing Out[62]= {0.375,62806} In[63]:= k - Total[Unitize[Total[Unitize[m1 - m2], {2, 3}]]] // Timing Out[63]= {0.344,62806} Carl Woll Wolfram Research
- References:
- Re: style question
- From: Bill Rowe <readnewsciv@sbcglobal.net>
- Re: style question