mdI0 =: 4 $. $.
   mdI1 =: $ #: I.@:,
   
   arr  =: 3 3 $ 1 0 0 0 1 0 0 0 1
   
   mdI0 arr
0 0
1 1
2 2
   mdI1 arr
0 0
1 1
2 2
   

The reason this isn't perfectly straightforward in J is that we tend to treat 
items of an array as equals; peers; and so it's unusual to pick apart their 
members from above. 

Put another quay, usually we delegate the management of an item's members to 
that item itself (if that makes sense to you). Anyway, that's what the ravel in 
mdI1 is expressing. 

The sparse matrix trick is just pressing a feature of $. into a novel use (I 
believe I picked up it up for the first time from Roger, or maybe Kirk Iverson, 
but it had been floating around a while before I ever saw it).

-Dan
 

Sent from my iPhone
 Th
> On May 31, 2014, at 9: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

Reply via email to