Hi Jon,
A quick first hack at it gave me this, but there are probably more elegant ways
to come.
(,@:~:&0 # ([: ,@{ <@i."0)@$) 3 3$1 0 0 0 1 0 0 0 1
┌───┬───┬───┐
│0 0│1 1│2 2│
└───┴───┴───┘
(,@:~:&0 # ([: ,@{ <@i."0)@$) 3 5$1 0 0 0 1 0 0 0 1
┌───┬───┬───┬───┬───┐
│0 0│0 4│1 3│1 4│2 3│
└───┴───┴───┴───┴───┘
(,@:~:&0 # ([: ,@{ <@i."0)@$) 2 3 5$1 0 0 0 1 0 0 0 1
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│0 0 0│0 0 4│0 1 3│0 1 4│0 2 3│1 0 2│1 0 3│1 1 2│1 2 1│1 2 2│
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘
(,@:~:&0 # ([: ,@{ <@i."0)@$) 1 3 5$1 0 0 0 1 0 0 0 1
┌─────┬─────┬─────┬─────┬─────┐
│0 0 0│0 0 4│0 1 3│0 1 4│0 2 3│
└─────┴─────┴─────┴─────┴─────┘
Cheers, bob
On May 31, 2014, at 6:57 AM, Jon Hough <[email protected]> wrote:
> Probably a very simple question. For a single dimension array I can do
> I. 1 0 1 1 1 0 0 0 1 0 1
> 0 2 3 4 8 10
> to get the nonzero indices.
> But I am not sure how to do this for a larger dimension. i.e. get the (i,j)
> or (i,j,k) index of nonzero elements.
> e.g. for this matrix:
> arr =. 3 3 $ 1 0 0 0 1 0 0 0 1
> I. doesn't work because it only gives me the ith value of the position of the
> nonzero elements.
> Any help appreciated.
> Regards.
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm