I want to generate all-length subsets of a vector, always starting from the
left-most integer:

We need to define a verb f, that does the following:

f 1 2 3 4 5 6

┌─┬───┬─────┬───────┬─────────┬───────────┐

│1│1 2│1 2 3│1 2 3 4│1 2 3 4 5│1 2 3 4 5 6│

└─┴───┴─────┴───────┴─────────┴───────────┘

It would be handy if it took a left argument as an option (partition count
for each partition), but that's not critical if that is too complex,

2 4 5 f 1 2 3 4 5 6

┌───┬───────┬─────────┐

│1 2│1 2 3 4│1 2 3 4 5│

└───┴───────┴─────────┘

More importantly, f needs to work on rank 2 arrays:

]n=.>:perm 3

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1


f"1 n

┌─┬───┬─────┐

│1│1 2│1 2 3│

├─┼───┼─────┤

│1│1 3│1 3 2│

├─┼───┼─────┤

│2│2 1│2 1 3│

├─┼───┼─────┤

│2│2 3│2 3 1│

├─┼───┼─────┤

│3│3 1│3 1 2│

├─┼───┼─────┤

│3│3 2│3 2 1│

└─┴───┴─────┘

I know I can do this by brute force:


Build dyadic verb t that takes the first x elements from y & boxes them:

t=.4 :'x {."1 ea {y'


(1 t n),.(2 t n),.(3 t n)

┌─┬───┬─────┐

│1│1 2│1 2 3│

├─┼───┼─────┤

│1│1 3│1 3 2│

├─┼───┼─────┤

│2│2 1│2 1 3│

├─┼───┼─────┤

│2│2 3│2 3 1│

├─┼───┼─────┤

│3│3 1│3 1 2│

├─┼───┼─────┤

│3│3 2│3 2 1│

└─┴───┴─────┘

Is there a more elegant (and efficient) way to do this?


Skip

Skip Cave
Cave Consulting LLC
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to