Below is a problem that I have enjoyed working on that stems from an attempt to
generalise the writing of a boxed table of mixed type to an Excel worksheet.
My attempt at a solution in the script at the bottom gives a non-optimal answer
(it only finds blocks within rows) that I'd like to improve if
possible/practical.
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.
Below is an example desired output of a verb "blocks" for tst1:
blocks tst1
0 3 NB. topleft of block
1 3 NB. shape of block
2 0
1 3
3 3
2 3
5 0
5 3
7 3
5 3
$blocks tst1
5 2 2
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.
tst2=: 5 [EMAIL PROTECTED] NB. Another example table of 0s & 1s that blocks
should work with
NB.========Start of Script======================================
ischar =: 3!:0 e. 2 131072"_
firstones=: > 0: , }: NB. Mask of first ones
tlcols=: <@[EMAIL PROTECTED]"1 NB. leftmost columns of blocks of 1s
toplefts=: i.@:# ,.&.> tlcols NB. topleft index of blocks of 1s
bytype=: 1 : 'u;.1~ (1, 2~:/\ ])'
lens=: <@({.bytype # #bytype)"1 NB. lengths of blocks of 1s
shapes=: 1: ,. &.> lens NB. shapes of blocks of 1s
blocks=: ;@toplefts ,:"1 ;@shapes NB. blocks of 1s
Note 'testing'
;toplefts tst1 NB. list of topleft of blocks of 1s
;toplefts -.tst1 NB. list of topleft of blocks of 0s
(;@toplefts ,:"1 ;@shapes) tst1 NB. List of blocks
;(toplefts ,:"1 &.> shapes) tst1 NB. Also list of blocks
(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==========================================
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm