sorry doesn't quite work when multiple 1s in row.
this does:
; (i.@:# , each each I.each) <"1 ] 0,. 4 3 $ 1 0 0 0 1 0 0 1 1 0 0 0
┌───┬───┬───┬───┐
│0 1│1 2│2 2│2 3│
└───┴───┴───┴───┘
I2d =: [: ; [: (i.@:# , each each I.each) [: <("1) 0 ,. ]
I2d 4 3 $ 1 0 0 0 1 0 0 1 1 0 0 0
┌───┬───┬───┬───┐
│0 1│1 2│2 2│2 3│
└───┴───┴───┴───┘
I2d 4 3 $ 1 0 0 0 1 0 0 0 1 0 0 0
┌───┬───┬───┐
│0 1│1 2│2 3│
└───┴───┴───┘
----- Original Message -----
From: 'Pascal Jasmin' via Programming <[email protected]>
To: "[email protected]" <[email protected]>
Cc:
Sent: Saturday, May 31, 2014 10:25:50 AM
Subject: Re: [Jprogramming] Getting index in 2+d array
The real problem with I. is that it returns 0 whether the first element is 1,
or no elements are 1.
But this works:
I. 0,. 4 3 $ 1 0 0 0 1 0 0 0 1 0 0 0
1
2
3
0
(i.@:# ,. I.) 0,. 4 3 $ 1 0 0 0 1 0 0 0 1 0 0 0
0 1
1 2
2 3
3 0
the column indexes (2nd col) are off by 1, and 0 means not found. So:
I2d =: [: ({. , <:@:{: )"1 [: (#~ 0<{:"1) [: (i.@:# ,. I.) 0 ,. ]
I2d 4 3 $ 1 0 0 0 1 0 0 0 1 0 0 0
0 0
1 1
2 2
I don't know of a better way... but I'd be surprised if there is a solution
short enough not to save
----- Original Message -----
From: Jon Hough <[email protected]>
To: "[email protected]" <[email protected]>
Cc:
Sent: Saturday, May 31, 2014 9:57:43 AM
Subject: [Jprogramming] Getting index in 2+d array
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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm