Creating multiple rows in a matrix

Hi, How do I create multiple rows in a matrix?

Roblox does not currently have a builtin matrix datatype which supports its respective mathematical operations, but you can easily represent one in Lua with a 2 dimensional array.

how do i do a 2d array nd read from them?

A simple 2d array would be constructed by inserting n number of arrays into a single array, each containing m number of objects in order to create a n*m matrix. You can similarly read values from this table by indexing it with the [] operator:

local matrix = {
    {1,  2,  3},
    {4,  5,  6},
    {7,  8,  9}
};

print(matrix[2][3])  --prints 6: the 2nd row, 3rd column
3 Likes

How would I generate multiple rows?

In the code sample above, the rows are hardcoded into the matrix. If you wanted to generate rows on the fly, you should insert a table into another using table.insert(target,value).

local matrix = {};  --Empty

for i=1,3 do  --Will loop three times
    table.insert(matrix,{0,0,0});  --Insert a row full of zeroes into your matrix
end

--[[

After, your matrix will look like this:

{
    {0,  0,  0},
    {0,  0,  0},
    {0,  0,  0}
}

]]
1 Like
local matrix = {{5,5,5},{3,3,3},{2,2,2}}

print(matrix[1][2])