Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!mach1!torn!nott!bnrgate!bnr.co.uk!uknet!pipex!uunet!haven.umd.edu!darwin.sura.net!news-feed-1.peachnet.edu!umn.edu!csus.edu!sfsuvax1.sfsu.edu!vpcsc4
From: vpcsc4@sfsuvax1.sfsu.edu (Emmett McLean)
Subject: Re: APL2 programming contest: The fastest #contab# function
Message-ID: <1993May12.091137.27283@csus.edu>
Sender: news@csus.edu
Organization: San Francisco State University
References: <Berni.9.0@nw42.wiwi.uni-bielefeld.de>
Date: Wed, 12 May 1993 09:11:37 GMT
Lines: 60

In article Bernhard Strohmeier writes:
>Hello,
>I am looking for the fastest APL2 function which computes the contingency 
>table from a (n x 2)-matrix of sample pairs of discrete random variables X 
>and Y.
>
>Example:  
>MAT      
>  2 2       
>  2 3      
>  3 4      
>  1 3      
>  3 4     
> -3 1    
>0.2 2
>  3 4
>  2 2
>  1 3
>  
>contab MAT
>   
>X\Y  1  2  3  4  
>-3  .1  0  0  0  
>.2   0 .1  0  0
> 1   0  0 .2  0
> 2   0 .2 .1  0
> 3   0  0  0 .3
>
  
 Compare your result to this J verb.
 
 a =. 10 2 $ 2 2 2 3 3 4 1 3 3 4 _3 1 0.2 2 3 4 2 2 1 3
 c =.  {@(;~)@(/:~)@~.@, +/ .="0 1 <"1
 d =. /:~@~.@, (' '&;@,.@[ ,. ]) /:~@~.@, ({. ,.@; }.)@":@, {@(;~)@(/:~)@~.@, +/ .="0 1 <"1
   

 test =. 6!:2,7!:2@]
   c a
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 2 0
0 0 0 2 1 0
0 0 0 0 0 3
0 0 0 0 0 0
   d a
+---+--------------+
|   |_3 0.2 1 2 3 4|
+---+--------------+
| _3| 0   0 1 0 0 0|
|0.2| 0   0 0 1 0 0|
|  1| 0   0 0 0 2 0|
|  2| 0   0 0 2 1 0|
|  3| 0   0 0 0 0 3|
|  4| 0   0 0 0 0 0|
+---+--------------+
   test 'd a'
0.12 4792
   test 'c a'
0.1 3948
   
