5 4 $ i.20 can be given as i.5 4 Dan's method is better, but the following also works:
0 2 0 1 2 } |: i.5 4 0 6 8 13 18 On Sat, May 22, 2010 at 10:22 PM, Dan Bron <[email protected]> wrote: > vec {"0 1 tab > or, simpler & more general, > vec {"_1 tab > > -Dan > > Please excuse typos; composed on a handheld device. > > -----Original Message----- > From: Roman Odaisky <[email protected]> > Date: Sat, 22 May 2010 17:17:33 > To: <[email protected]> > Subject: [Jprogramming] An element from each row of a table > > Hi, > > I’ve only recently gotten into J. It’s very interesting but I’m often > unable > to find simple solutions to simple problems. I’m now struggling with > finding a > way of taking a table and a vector and picking elements from the table with > positions in rows according to the vector, that is, the i-th element of the > vector specifies the element to be taken from the i-th row of the table. > How > can this be done? > > For example, given table =: 5 4 $ i. 20 and positions =: 0 2 0 1 2, the > result > should be 0 6 8 13 8: > > <0> 1 2 3 > 4 5 <6> 7 > <8> 9 10 11 > 12<13>14 15 > 16 17<18>19 > > The best I was able to come up with is > > pick =: {~ (<"1 @: ,.~ i.@:#) > table pick positions > 0 6 8 13 18 > > which is rather ugly. I expected this to look more like “positions > {"something > table” but nothing of that kind worked. > > In Python that would look like this: > > def pick(table, positions): > return [row[p] for row, p in zip(table, positions)] > > or in a J-ish point-free way: > > pick = compose(partial(starmap, getitem), zip) > > It looks like no more than five verbs should be necessary. How can J do > this? > > -- > TIA > Roman. > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
