[comp.lang.apl] EEM responds about histograms

loc@yrloc.ipsa.reuter.COM (Leigh Clayton) (04/03/91)

 I pass the following along from Eugene McDonnell on IPSA mailbox...
--------------------------------------------------------------------------

no. 5065227 filed  1.46.50  tue  2 apr 1991
from eem
to   loc
cc   jig
subj histogram

Given an array y of nonnegative integers, with maximum value x-1,
a histogram h giving the number of occurrences of each possible
value in the array may be obtained in J by:

    h =. <: , #/.~ (,y),i. x

By JOINING to the RAVEL of y a vector (i. x) of all possible
values, we are insured that the result contains a slot for each
possible value, whether it appears in y or not. This argument is
used (by virtue of the DUPLICATE adverb ~) as left and right
argument to COUNT (#) by KEY (/.), giving a count of the
occurrences of each possible value in the argument (the argument
is its own key). RAVELLING this and SUBTRACTING 1 (<:) from it
(to allow for the presence of i. x in the argument, which should
not add to the count) gives the desired result.

-----------------------------------------------------------
loc@tmsoft.UUCP                     uunet!mnetor!tmsoft!loc
loc@yrloc.ipsa.reuter.COM                   (Leigh Clayton)