Instead of the parenthesis and putting the positions is front
((i.5),&.> positions){table
It is easy to use ~ to reverse the operation
table {~ (i.5),&.> positions
0 6 8 13 18
And if you want to put it in a verb
from=. 4 : 0
x{~(i.5),&.>y
)
table from positions
0 6 8 13 18
2010/5/23 Björn Helgason <[email protected]>:
> table =: 5 4 $ i. 20
>
> positions =: 0 2 0 1 2
>
> (0 0;1 2;2 0;3 1;4 2){table
> 0 6 8 13 18
>
> (i.5),&.> positions
> ┌───┬───┬───┬───┬───┐
> │0 0│1 2│2 0│3 1│4 2│
> └───┴───┴───┴───┴───┘
>
> ((i.5),&.> positions){table
> 0 6 8 13 18
>
> 2010/5/22 Roman Odaisky <[email protected]>:
>> 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
>
>
>
> --
> Björn Helgason, Verkfræðingur
> Fornustekkum II
> 781 Hornafirði,
> t-póst: [email protected]
> gsm: +3546985532
> sími: +3544781286
> http://groups.google.com/group/J-Programming
>
>
> Tæknikunnátta höndlar hið flókna, sköpunargáfa er meistari einfaldleikans
>
> góður kennari getur stigið á tær án þess að glansinn fari af skónum
> /|_ .-----------------------------------.
> ,' .\ / | Með léttri lund verður |
> ,--' _,' | Dagurinn í dag |
> / / | Enn betri en gærdagurinn |
> ( -. | `-----------------------------------'
> | ) | (\_ _/)
> (`-. '--.) (='.'=) ♖♘♗♕♔♙
> `. )----' (")_(") ☃☠
>
--
Björn Helgason, Verkfræðingur
Fornustekkum II
781 Hornafirði,
t-póst: [email protected]
gsm: +3546985532
sími: +3544781286
http://groups.google.com/group/J-Programming
Tæknikunnátta höndlar hið flókna, sköpunargáfa er meistari einfaldleikans
góður kennari getur stigið á tær án þess að glansinn fari af skónum
/|_ .-----------------------------------.
,' .\ / | Með léttri lund verður |
,--' _,' | Dagurinn í dag |
/ / | Enn betri en gærdagurinn |
( -. | `-----------------------------------'
| ) | (\_ _/)
(`-. '--.) (='.'=) ♖♘♗♕♔♙
`. )----' (")_(") ☃☠
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm