It is not quite clear how *blocks* are defined: fully
surrounded by zeros, etc.

--- On Wed, 6/11/08, Sherlock, Ric <[EMAIL PROTECTED]> wrote:

I apologize for having a conversation with myself, but hopefully this is 
interesting for others too?
Please feel free to jump in if you have something to contribute!

Using the ideas in my last post I can significantly simplify my original 
working (but sub-optimal) script.

If blocks were column- rather than row-oriented then it would be better to
redefine tls & bls as:
tls=: [: indices firstones NB. topleft index of blocks of 1s
brs=: [: indices lastones  NB. bottomright index of blocks of 1s

NB.========Start of Script======================================
ischar=: 3!:0 e. 2 131072"_
firstones=: > 0: , }:
lastones=: > 0: ,~ }.
indices=: $ #: I.@,   NB. get row,.col indices of 1s in matrix

tls=: [: indices firstones"1 NB. topleft index of blocks of 1s
brs=: [: indices lastones"1  NB. bottomright index of blocks of 1s

shapes=: [: >: brs - tls  NB. shapes of blocks of 1s
blocks=: tls ,:"1 shapes  NB. blocks of 1s

Note 'testing'
tls tst1          NB. list of topleft of blocks of 1s
tls -.tst1        NB. list of topleft of blocks of 0s
blocks tst1       NB. list of blocks of 1s

(blocks   ischar &> tsta) <;.0 tsta  NB. blocks of char (you will need to 
create a tsta to run this)
(blocks -.ischar &> tsta) <;.0 tsta  NB. blocks of non-char
)
NB.=======End of Script==========================================

> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Sherlock, Ric
> Sent: Thursday, 12 June 2008 11:44
> To: Programming forum
> Subject: RE: [Jprogramming] Finding blocks in a table/matrix
>
> Based on an idea from Mike Day (off-forum) I have come up
> with the following approach that could be extended to produce
> a correct solution, but it will only work as long as there
> are no over- or underhanging or regions.
> Anyone have an approach that will handle those cases too?
>
> firstones=: > 0: , }:
> lastones=: > 0: ,~ }.
>
> tl=: (firstones *. firstones"1)  NB. topleft
> br=: (lastones *. lastones"1)    NB. bottomright
> indices=: (1 , {:@$) *"1 [: (<. ,. 1&|) {:@$ %~ [: I. ,
> NB. gets row,.col indices of 1s in a table
>
> ---Sherlock, Ric wrote:
> > tsta is a boxed table(matrix) of mixed numeric and literal type
> >
> > ischar=: 3!:0 e. 2 131072"_
> > tst1=: ischar &> tsta  NB. Mask of boxed literals in tsta
> >
> > NB. Below is an example tst1 for copying into a session.
> > tst1=: _99&".;._2 (0 : 0)
> > 0 0 0 1 1 1
> > 0 0 0 0 0 0
> > 1 1 1 0 0 0
> > 0 0 0 1 1 1
> > 0 0 0 1 1 1
> > 1 1 1 0 0 0
> > 1 1 1 0 0 0
> > 1 1 1 1 1 1
> > 1 1 1 1 1 1
> > 1 1 1 1 1 1
> > 0 0 0 1 1 1
> > 0 0 0 1 1 1
> > )
> >
> > The problem is to specify the blocks of ones in the table.
> > A block is specified by the position of its top left member
> > and its shape.
> ...
> > Obviously there are other ways to organise the blocks of ones
> > in tst1. A solution is correct as long as all ones in tst1
> > are included in one and only one block. Solutions that
> > produce fewer blocks are better provided they don't take much
> > longer to find the minimum number of blocks.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to