I. ,map
8 11 25
]ixs=. ($map) #: I. ,map
1 2
1 5
4 1
map{~<"1 ixs
1 1 1
The same expression works for higher-dimensional arrays as well.
On Sun, Jun 7, 2020 at 12:41 PM <xash@λ.land> wrote:
> Hello everyone,
>
> I'm new to J and while working with 1d indices feels quite natural, using
> multidimensional indices not so. For example, finding the indices
> (3 2$1 2,1 5,4 1) of #'s in this bit mask^Wmap:
>
> map=:'#'= [;._2 (0 : 0)
> ......
> ..#..#
> ......
> ......
> .#....
> ......
> )
>
> With 1d indices it would be `linear=:I.;map`. But to get the 2d indices the
> result of I. needs to be intertwined with row numbers as I. only
> operates on lists:
> points=:; (i.#map) ,"0/each <&I. map
> – and this won't even generalize to more dimensions.
>
> To reverse this and built back a bit map, instead of `1 linear}($map)$0`
> I'd use
> 1 (<"1 points)}($map)$0
> though this is wrong if points='', as then with `1 (<'')}…` the whole
> map is set to 1. So it's actually
> 1 (<"1^:(*@#) points)} ($map)$0
>
> With the 2d version of `linear{map` it's even more complicated, as `''{map`
> returns not nothing but an empty axis with shape 0 6, so output needs to be
> checked, too.
>
> Before I built up too many inefficient helper functions: what is the usual
> approach to this? I could drop to 1d indices and convert when needed
> (what is the manhattan distance between 12 and 16?), but for this I then
> need
> the dimensions of map all over the place to get back x/y coordinates and
> this doesn't feel right either.
>
> Thanks!
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
Devon McCormick, CFA
Quantitative Consultant
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm