Hi, Given an array index vector i, how would you go about finding efficiently all indices (or elements at them) of an array for which the index in each dimension is either the same as that in i, or is offset by +/- n, where n is the same for all dimensions, and keep them grouped by “direction”?
I have a solution but it isn’t very pretty. I will use it to illustrate though, as this is kind of hard to explain: ugly=: 4 : '(}.dirs#x) <@}.@}:@(+^:(] -: x | ])^:a:)"1 y' dirs=: (1|.i:1)&([: ,/ ,"0 1/)&(i.1 0) demo=: ugly (#@> # >:@i.@#)@[`(<"1@;@[)`]} [ $ 0: 6 7 ugly 3 4 ┌───┬───┬───┬───┬───┬───┬───┬───┐ │3 5│3 3│4 4│4 5│4 3│2 4│2 5│2 3│ │3 6│3 2│5 4│5 6│5 2│1 4│1 6│1 2│ │ │3 1│ │ │ │0 4│ │0 1│ │ │3 0│ │ │ │ │ │ │ └───┴───┴───┴───┴───┴───┴───┴───┘ 6 7 demo 3 4 0 8 0 0 6 0 0 0 0 8 0 6 0 7 0 0 0 8 6 7 0 2 2 2 2 0 1 1 0 0 0 5 3 4 0 0 0 5 0 3 0 4 _13 ]\ 5 5 5 ugly 2 2 2 ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ │2 2 3│2 2 1│2 3 2│2 3 3│2 3 1│2 1 2│2 1 3│2 1 1│3 2 2│3 2 3│3 2 1│3 3 2│3 3 3│ │2 2 4│2 2 0│2 4 2│2 4 4│2 4 0│2 0 2│2 0 4│2 0 0│4 2 2│4 2 4│4 2 0│4 4 2│4 4 4│ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ │3 3 1│3 1 2│3 1 3│3 1 1│1 2 2│1 2 3│1 2 1│1 3 2│1 3 3│1 3 1│1 1 2│1 1 3│1 1 1│ │4 4 0│4 0 2│4 0 4│4 0 0│0 2 2│0 2 4│0 2 0│0 4 2│0 4 4│0 4 0│0 0 2│0 0 4│0 0 0│ └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ <"2 ] 5 5 5 demo 2 2 2 ┌────────────┬────────────┬─────────┬────────────┬────────────┐ │26 0 24 0 25│0 0 0 0 0│8 0 6 0 7│0 0 0 0 0│17 0 15 0 16│ │ 0 0 0 0 0│0 26 24 25 0│0 8 6 7 0│0 17 15 16 0│ 0 0 0 0 0│ │20 0 18 0 19│0 20 18 19 0│2 2 0 1 1│0 11 9 10 0│11 0 9 0 10│ │ 0 0 0 0 0│0 23 21 22 0│0 5 3 4 0│0 14 12 13 0│ 0 0 0 0 0│ │23 0 21 0 22│0 0 0 0 0│5 0 3 0 4│0 0 0 0 0│14 0 12 0 13│ └────────────┴────────────┴─────────┴────────────┴────────────┘ The different numbers in the arrays represent the different “directions” I was referring to. What I need is not necessarily the indices of these positions; any way to separate them is good. Louis ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
