Devon wrote:
> We must be on the same wavelength lately
> I came up with pickFrom=: ] {~ [ ?@$ [: # ]
Devon, we're on the same wavelength but in a different phase. Your code
picks a random item from a list (and could leverage a hook if you like:
pickFrom=: ] {~ (?@$ #) ).
My code does something similar, except not all indices are equally likely:
in my situation, the probability of selecting a given index is weighted by
its value.
Brian wrote:
> The dyadic only form is as follows. Notice there is no sort or I. .
> 2((([?@#+/@]){(;@((#~)</.]# ;@(i.@#)))@])) 5 3 4 2
That's an interesting approach, and notationally correct (i.e. in the
abstract, it solves the problem). But in practical terms it has certain
limitations. The interface only provides for integral arguments, and
performance-wise it wouldn't scale easily with their magnitude. For
example, 1000 2000 3000 would take much longer than 1 2 3, and 0.1 0.2 0.3
wouldn't work at all.
(And I have a suspicion it might have issues with sequences of equal
probabilities, e.g. 1 3 3 3 4 5 6, but I haven't tested that).
-Dan
PS: For anyone interested in the context of this question, it was raised by
Max Harms, Tracy's son. He's learning J and applying it to AI (neural nets
& such). See here for the results of a twitter discussion on the topic:
http://rosettacode.org/wiki/K-means%2B%2B_clustering
Improvements to the J solution are welcome, whether to the random index
generator or elsewhere.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm