I am having problems understanding what you are asking for. I'll try creating some examples which represent my understanding of what you seem to be asking for, and maybe that can help you tell me where I've gone off track:
"Given an array index vector i," For example: i=: i.4 5 "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" This sounds like all values from i, if I take it literally. but, let's say I want column 2 and row 2 ($ 2&e.@#:"1 0 ]) i 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 0 The indices would be I.,($ 2&e.@#:"1 0 ]) i 2 7 10 11 12 13 14 17 Except we've now got several conflicting uses of the word index: row index, column index, ravel index. "or is offset by +/- n, where n is the same for all dimensions, and keep them grouped by “direction”?" Offset from what? Let's say i represents a 10 by 10 matrix, and that want row or column 6, or an offset of 2 from either of those: ($ +./@((6+_2 0 2)&e.)@#:"1 0 ]) i.10 10 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 But I don't know which direction the diagonal elements would belong to. (And then I hit your examples and realize that I have no idea how any of that relates to what I thought you were asking for.) Anyways, maybe try again? Thanks, -- Raul On Tue, Aug 15, 2017 at 2:45 PM, Louis de Forcrand <[email protected]> wrote: > 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 ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
