[Jprogramming] Boxes of different lengths
Raul Miller
rauldmiller at gmail.com
Tue Jan 10 23:45:11 HKT 2012
DATA=: ".;._2]0 :0 0 0 1 1 1 1 2 2 2 2 2 4 4 4 5 5 5 0 7 8 9 2 2 5 5
5 3 8 0 0 5 6)
(</./ #inv~ (e.~ i.@(1 + >./))@{. ) DATA
+---+-------+---------++-----+---+
|5 0|7 8 9 2|2 5 5 5 3||8 0 0|5 6|
+---+-------+---------++-----+---+
However, this is quirky -- it assumes that you do not want any empty
boxes on the right hand side of the result. If you want those empty
boxes you will need a way of specifying their presence:
6 (</./@] #inv~ i.@[ e. {.@] ) DATA
+---+-------+---------++-----+---+
|5 0|7 8 9 2|2 5 5 5 3||8 0 0|5 6|
+---+-------+---------++-----+---+
Here, the left argument tells us the length of the argument.
Then again, you could achieve the same thing with my first expression:
6 {. (</./ #inv~ (e.~ i.@(1 + >./))@{. ) DATA
+---+-------+---------++-----+---+
|5 0|7 8 9 2|2 5 5 5 3||8 0 0|5 6|
+---+-------+---------++-----+---+
I could not choose between these options without better understanding
your application.
FYI,
--
Raul
On Tue, Jan 10, 2012 at 9:40 AM, Ben Gorte - LR <B.G.H.Gorte at tudelft.nl> wrote:
> Dear Forum,
>
> Perhaps it is a beginners question, but somehow I cannot come up with a decent answer.
>
> I have a matrix like of position-value pairs like:
>
> 0 0 1 1 1 1 2 2 2 2 2 4 4 4 5 5
> 5 0 7 8 9 2 2 5 5 5 3 8 0 0 5 6
>
> (it could also be transposed), and I would like to have a list of boxes:
>
> +---+-------+---------++-----+---+
> |5 0|7 8 9 2|2 5 5 5 3||8 0 0|5 6|
> +---+-------+---------++-----+---+
>
> So the values are placed in boxes according to the positions.
>
> My matrix above is sorted (from left to right), but probably this is not essential for most solutions. Furthermore, the order of the elements within the boxes of the result is not important for the remainder of my algorithm.
>
> I have a solution that loops through the matrix from left to right, but it is way too slow (and it does not look like J). Isn't there a better way?
>
> Thanks,
> Ben
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list