Re: Re: style question
- To: mathgroup at smc.vnet.net
- Subject: [mg79482] Re: [mg79424] Re: style question
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Fri, 27 Jul 2007 06:03:41 -0400 (EDT)
- References: <f84jp8$qmp$1@smc.vnet.net> <f86rat$pfm$1@smc.vnet.net> <18414151.1185447263555.JavaMail.root@m35>
- Reply-to: drmajorbob at bigfoot.com
Vince has one of the level specs wrong (for the OP's intent), and Carl Woll's solution is faster anyway: k = 10^6; m1 = RandomInteger[1, {k, 2, 2}]; m2 = RandomInteger[1, {k, 2, 2}]; (* the OP's method *) Timing[ Count[Thread[temporary[m1, m2]] /. temporary -> Equal, True]] {4.312, 62711} (* Vince Virgilio with corrected level spec *) Timing[Count[MapThread[Equal, {m1, m2}, 1], True]] {3.235, 62711} (* Carl Woll *) Count[Total[Unitize[m1 - m2], {2, 3}], 0, {1}] // Timing {0.406, 62711} Carl's method also works for k=10^7 on my machine, whereas the other two methods shut down the kernel with "No more memory available." k = 10^7; m1 = RandomInteger[1, {k, 2, 2}]; m2 = RandomInteger[1, {k, 2, 2}]; Count[Total[Unitize[m1 - m2], {2, 3}], 0, {1}] // Timing {4.203, 625127} Bobby On Thu, 26 Jul 2007 04:37:28 -0500, Vince Virgilio <blueschi at gmail.com> wrote: > On Jul 25, 2:41 am, Vince Virgilio <blues... at gmail.com> wrote: >> On Jul 24, 6:20 am, Yaroslav Bulatov <yarosla... at gmail.com> 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] >> >> MapThread should avoid eager evaluation of Equal. I am away from >> Mathematica, so cannot test this: >> >> matches = MapThread[Equal, {m1, m2}]; >> >> (I wonder if MapThread was created specifically to mitigate evaluation >> issues.) >> >> Vince Virgilio > > Correction to self. I overlooked the three-dimensionality of m1 and > m2. Again I'm without Mathematica; still you might try: > > matches = MapThread[Equal, {m1, m2}, 3]; > Count[matches, True, {3}] > > By the way, your syntax for the first argument of RandomInteger > appears to be undocumented. > > Vince Virgilio > > > -- DrMajorBob at bigfoot.com