Thanks that's great!
I've documented my deconstruction of your code below in case it is of interest 
to others.

---R.E. Boss wrote:
>    T2=: [:($$ [:+/\ ,) (1, }:~: }.)"1

Find the first 1s in each group by row, then ravel and find the running sum and 
reshape back to original shape.

>    [;/'H V'=: (*"_ _1 T2,: T2&.|:) tst1
Run T2 on tst1 and on its transpose to find the horizontal and vertical groups 
then laminate the two results and multiply each one by tst1 to zero all zero 
positions.

>    rpl=:] - (-/ , 0:)@[ {~ [EMAIL PROTECTED] i. ]
Replace occurrences of {.x in y with {:x

>  R=:  V ( [EMAIL PROTECTED] $]) i.~@,.&,/ (([:(,:|.) ,"_1) rpl"_1  ])H,:~V
>         NB. indicating blocks of 1's
I'll break this one up into the 3 separate main verbs

 res1=: (([:(,:|.) ,"_1) rpl"_1  ])H,:~V
Replace numbers in V with the number in H that corresponds with the first 
occurrence of the number in V. Vice versa for H. I'm not if there is a reason 
for H,:~V instead of V,:H?

 res2=:  i.~@,.&,/ res1
Stitch the ravel of each of the V and H matrices to create a 2-column matrix.
Find the first occurrence of each unique row of that matrix.

 R=: V ( [EMAIL PROTECTED] $]) res2
Reshape res2 to the shape of V (and tst1).

>
> [R=: V ([ (,:&, rpl [) [EMAIL PROTECTED] $]) i.~@,.&,/ (([:(,:|.) ,"_1) 
> rpl"_1  ])H,:~V
>         NB. less blocks, but if H is used as LHS, answer is wrong ...
Haven't looked at this one yet, but appears to be similar with an extra replace 
operation.

>  ([:([,: (-~>:))/"_1 $#: (](i.}.@,. i:) 0~.@,])@,) R

idx_az=: $#: (](i.}.@,. i:) 0~.@,])@,) R
Find first and last occurrences in the ravel of R of its nub. Zero prepended to 
nub so can drop later.
Stitch them as 2-column matrix and drop first row (first & last index of zeros).
Decode the ravel indexes using shape of R to give the row and column indices of 
the top-left and bottom-right corners of each rectangle of numbers.

tl_shape=: ([:([,: (-~>:))/"_1 idx_az
For each item (2x2 matrix) of idx_az, laminate the top-left row and column 
indices to the calculated shape (rows and columns) of rectangle.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to