# Transposing this data structure to a matrix?

Say I have a data structure which looks like thls

``````{
3,
1,1,1,
1,0,1,
1,1,1
}
``````

Will name that table “feed”.

The first index defines the square root size of the matrix. The matrix should return a table in which there is arrays in it. The amount of arrays is the value of the first element in feed (3). Assume the language I am using indexes stuff with the first element being 1 (not 0). Inside each of these arrays will hold the same amount of values as the amount of arrays inside our matrix. What would the pseudo code of this function look like? (Feel free to ask further questions!)

``````local feed = {
3,
1,1,1,
1,0,1,
1,1,1
}
local module = {}
local function convertFeedToMatrix(input)
local m = {}
for x=1,input do
m[x] = {}
for y=1,input do
m[x][y] = feed + (x-y)
end
end
return m
end
``````

The element at `(x,y)` would be at the position `y * n + x + 1`, with `n` being the matrix size, and the `+ 1` due to the first array element being used to define the size.
So to do that we would have to do some offsetting to account for Lua’s arrays starting at 1 rather than 0.

``````local feed = {
3,
1,2,3,
4,0,5,
6,7,8
}
local module = {}
local function convertFeedToMatrix(input)
local m = {}
for y = 0, input - 1 do
m[y + 1] = {}
for x = 0, input - 1 do
-- m[y + 1][x + 1] would be m[y][x] in a zero-index language
m[y + 1][x + 1] = feed[y * input + x + 2] -- would be plus 1 in a zero index language
end
end
return m
end

local function printMatrix(input)
for y = 1, #input do
print(table.concat(input[y], ", "))
end
end

local matrix = convertFeedToMatrix(feed)

printMatrix(matrix)
--[[
1, 2, 3
4, 0, 5
6, 7, 8
]]
``````

I switched the order from x-y to y-x so that the result array would look similar to the input array:

``````{
{1, 2, 3},
{4, 0, 5},
{6, 7, 8}
}
``````