Or, if you want to preserve the elegance of the key solution:
(0{DATA) < /. 1{DATA
+---+-------+---------+-----+---+
|5 0|7 8 9 2|2 5 5 5 3|8 0 0|5 6|
+---+-------+---------+-----+---+
</. / DATA NB. Like Dan said...
+---+-------+---------+-----+---+
|5 0|7 8 9 2|2 5 5 5 3|8 0 0|5 6|
+---+-------+---------+-----+---+
AllKeys e. ~.0{DATA NB. Zeros where we're missing possible keys...
1 1 1 0 1 1 0
(AllKeys e. ~.0{DATA) #^:_1 ] </. / DATA NB. Put in empties
afterwards...
+---+-------+---------++-----+---++
|5 0|7 8 9 2|2 5 5 5 3||8 0 0|5 6||
+---+-------+---------++-----+---++
NB. " #^:_1 " is like APL's "expand" (\) - it's the inverse (^:_1) of
"compress" (#).
On Tue, Jan 10, 2012 at 11:30 AM, Devon McCormick <[email protected]>wrote:
> Beginner questions are welcomed. Here's one way to include empty boxes
> for keys without any values:
>
> ] DATA=: 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
>
> 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
> ~.0{DATA NB. Existing keys
> 0 1 2 4 5
> ]AllKeys=. i.7 NB. Assume these are all possible keys
> 0 1 2 3 4 5 6
> NB. Extra ....... keys, dummy ....... values corresponding to extras,
> }. &.> (AllKeys,0{DATA) < /. AllKeys,1{DATA
> +---+-------+---------++-----+---++
>
> |5 0|7 8 9 2|2 5 5 5 3||8 0 0|5 6||
> +---+-------+---------++-----+---++
> NB. " }.&.> " drops dummies from result.
>
> On Tue, Jan 10, 2012 at 9:40 AM, Ben Gorte - LR <[email protected]>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
>>
>
>
>
> --
> Devon McCormick, CFA
> ^me^ at acm.
> org is my
> preferred e-mail
>
>
--
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm