Re: Re: style question
- To: mathgroup at smc.vnet.net
- Subject: [mg79495] Re: [mg79485] Re: style question
- From: Carl Woll <carlw at wolfram.com>
- Date: Sat, 28 Jul 2007 05:24:15 -0400 (EDT)
- References: <200707241008.GAA26147@smc.vnet.net> <f89rdm$6d1$1@smc.vnet.net> <200707271005.GAA03565@smc.vnet.net>
Peter Pein wrote:
>Carl Woll schrieb:
>
>
>
>>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
>>
>>
>>
>
>Hello Carl,
>
>doesn't Unitize[x]==0 imply x==0? And when only zeroes are counted, why
>the call to Unitize?
>
>I can not try to explore the difference (if any) because version 5.2
>does not allow Total with a range of levelspecs.
>
>Peter
>
>
Suppose the 2 x 2 matrices being subtracted are
{{0,1},{0,0}} - {{1,0},{0,0}}
{{-1,1},{0,0}}
If we total this matrix we'll get 0. Of course, these two matrices are
not the same, so we don't want to count this zero! By using Unitize, we
convert this to
{{1,1},{0,0}}
and then totaling gives 2, which is not 0. Instead of Unitize, one could
use Abs.
As for Total with level spec issue, you can use Transpose:
Total[Transpose[data, {3,2,1}], 2]
will do the same thing as Total[data, {2,3}].
Carl
- References:
- style question
- From: Yaroslav Bulatov <yaroslavvb@gmail.com>
- Re: style question
- From: Peter Pein <petsie@dordos.net>
- style question