Well, if you are talking about multiple extents, rather than variable extents, then you will have to use a temp table (or if you must, a work table).
If you know how many dimensions you will have, then your table could be defined like so (eg for 2 dimensions):
[pseudo code]
def temp-table matrix
field x
field y
field contents
if you are intending to reuse the construct, I would control access to it through a few methods, eg.
SetMatrix(x, y, contents)
GetMatrix(x, y)
but that's just me.
btw I'm not sure your method would work - how would you know key = 16 represented (4,4) and not (2,8), etc.