MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: style question


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


  • Prev by Date: Re: defining random variable with piecewise-continuous PDF Mathematica 6
  • Next by Date: Re: Cyclic permutations
  • Previous by thread: Re: style question
  • Next by thread: Re: style question