($tst) #: I. , tst

Works for any rank array, not just matrices.



----- Original Message -----
From: "Sherlock, Ric" <[EMAIL PROTECTED]>
Date: Wednesday, June 11, 2008 16:56
Subject: [Jprogramming] find row and column indices
To: Programming forum <[email protected]>

> In separate thread I needed to find the row and column indices 
> of 1s in a rectangular array.
> 
> tst=: _99&".;._2 (0 : 0)
> 0 0 0 1 0 0
> 0 0 0 0 0 0
> 1 0 0 0 0 0
> 0 0 0 1 0 0
> 0 0 0 0 0 0
> 1 0 0 0 0 0
> )
> 
>   indices tst
> 0 3
> 2 0
> 3 3
> 5 0
> 
> I came up with the following but feel I'm probably missing 
> something (much) simpler?
> 
> indices=: (1 , {:@$) *"1 [: (<. ,. 1&|) {:@$ %~ [: I. ,
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to