What is the benefit of (#~ -...@=&0) 1 0 2 0 3 0 0 4 5 0 0 1 2 3 4 5 Over: (-.&0) 1 0 2 0 3 0 0 4 5 0 0 1 2 3 4 5
? 2009/8/7 Alexander Mikhailov <[email protected]>: > > Hi Andrea, > > I'm also a newbie here. Let me try to help with your questions. > >> 1 - Counting elements line by line. >> >> I'm looking for the number of prime factors of a list of >> numbers If I was going for a single number i'd do: >> >> > #q:8 >> > 3 >> >> which is perfect. But I can't do it for a list of numbers, >> as q:1+i.10 returns a matrix and # returns the number of >> rows. How can I get the number of prime factor line by line? > > q:6 returns a list of rank 1, q:6 7 returns a matrix of rank 2. > # applied to the matrix count the rows; you have to apply # to > each row separately. # as monad has rank _, so you can change it: > > #"1 q:1+i.10 > 3 3 3 3 3 3 3 3 3 3 > > So, # counts not only prime factors, but also fillings, which > were created when results were joined into the matrix. You have to > apply # before the results are joined. You can do it this way: > > #...@q:1+i.10 > 0 1 1 2 1 2 1 3 2 2 > > This actually makes a single verb, #...@q: , which first does q: and > then does # with the result of q: . The rank of this verb is same > as the rank of q: . > >> 2 - Zeroes matter when counting. >> >> How can I eliminate items with value 0 from a list? >> i.e. given 1 0 2 0 3 0 0 4 5 0 0 >> 6 i'd like to obtain 1 2 3 4 5 6 >> > > This problem appears rather often in my experience. I've found the > following way to handle it: first I find out which elements are > not zeros and then fetch those elements from original array. Like > this: > t1 =. -. 0 = 1 0 2 0 3 0 0 4 5 0 0 > t1 # 1 0 2 0 3 0 0 4 5 0 0 > 1 2 3 4 5 > > Or it can be expressed in tacit form: > > (#~ -...@=&0) 1 0 2 0 3 0 0 4 5 0 0 > 1 2 3 4 5 > > Here =&0 - monad, which checks for equality to 0, -...@=&0 does the > same and then reverses the results - 0 makes 1, 1 makes 0, and #~ > applies the # after swapping x and y arguments. > > Alex > > > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
