How to concatenate matrices in lua with wrapping

79 Views Asked by At

im not sure if the title is the correct question, i have very little programming knowledge but i know exactly what i want to accomplish, not how to ask what to do, so ill just give an example.

mX = {
{X,0,0,0,X},
{0,X,0,X,0},
{0,0,X,0,0},
{0,X,0,X,0},
{X,0,0,0,X},
}

mY = {
{X,0,0,0,X},
{0,X,0,X,0},
{0,0,X,0,0},
{0,X,0,0,0},
{X,0,0,0,0},
}

i want to be able to combine them horizontally so i can make a bigger array, but wrap the input once it reaches a certain column count, for instance if i wanted to make

mZ = mX, mY, mX, mY, mX, mY, mX

mZ = {
{X,0,0,0,X,X,0,0,0,X,X,0,0,0,X,X,0,0,0,X,X,0,0,0,X},
{0,X,0,X,0,0,X,0,X,0,0,X,0,X,0,0,X,0,X,0,0,X,0,X,0},
{0,0,X,0,0,0,0,X,0,0,0,0,X,0,0,0,0,X,0,0,0,0,X,0,0},
{0,X,0,X,0,0,X,0,0,0,0,X,0,X,0,0,X,0,0,0,0,X,0,X,0},
{X,0,0,0,X,X,0,0,0,0,X,0,0,0,X,X,0,0,0,0,X,0,0,0,X},
{X,0,0,0,X,X,0,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,X,0,X,0,0,X,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,X,0,0,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,X,0,X,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{X,0,0,0,X,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
}

ive tried using chatgpt but i dont know how to word what i want to do correctly and giving it an example didnt really help much, i successfully got it to make something that can result in combining horizontally but as soon as i try to implement wrapping it to a new "row" of matrices it stops understanding what im trying to do. ive tried using table.insert but it breaks my matrix somehow when i try to use it, regardless i dont think it would be useful for a matrix. i gave up on trying to do this in a neat way and was going to just make a recursive function to manually add the values from one matrix to another but it seemed to shift existing values in the matrix over when i tried to add values expanding it.

edit 1: i managed to make a function with table insert to add the matrices together but using the matrices as arguments in the function ends up not using them seperately as i would assume but instead directly uses the matrices themselves, so if i use arguments A and B in a function that are tied to mX and mY respectively, instead of copying the matrices to A and B it just uses mX and mY

edit 2: i managed to get something working good enough the day after i posted this, as it works good enough for what im doing i probably wont improve it for a while, it doesnt wrap and start on the left when it goes to a new "row" but instead puts the next part into the center, which oddly enough works out perfectly

          k = 0
function catcat(kot,meow)
     if #kot[1 + k] >= 77 then
     k = k + 6
     end
      for i=1,#meow do
 for ii=1, #meow[i] do
     table.insert(kot[i + k],meow[i][ii])
 end
end
end

mKOT = {{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}{}}

i ran into a couple problems when combining matrices of different height, but since it isnt an immiediate issue im not worried about it, different length works fine, height is the main issue.

1

There are 1 best solutions below

1
Leol22 On

Are you familiar with for loops? It's safe to assume so since you are already working with matrices, but if you aren't then I would suggest looking them up. From my knowledge, which is far from infallible, there isn't a simple way to do this other than writing a for loop.

First off, the main thing you need to work on is how to join two tables toghether. I would do this by writing
for i=1,#table2,1 do
table1[#table1+1]=table2[i]
end
assuming your tables are 1-indexed.
This will go through table2 and add each item to the end of table1.
Use another for-loop to do this vertically.
There are definately more optimized ways of doing this, but since it appears as though you're still learning i think you should focus on getting the basics down.

Now, the wrapping might prove a bit harder. If you reach a certain line point, you will want to create 5 (in this case) new tables in mZ. I'm not super certain about the specifics, whether you want the wrap only between matrices or just whether a certain column is reached. In both cases, it should be a simple conditional.

I would personally use a "pointer" to indicate the top line of the currently-modified matrix.