Iterating through a 2d matrix?

So this isn’t working and i’m kinda stumped (I know the matrix isn’t empty)

local pixelMatrix = require(script.Parent.Modules.Setup)

local count = 0
for x=0,#pixelMatrix do
	for y=0,#pixelMatrix[x] do
		count += 1
	end
end
print(count)

helppls.rbxm (2.1 KB)

???

Something like this?

count = 0
for _, tab in pairs(matrix) do
     for _, item in pairs(tab) do
           count = count + 1
     end
end

print(count)

That defeats the whole purpose, so no

EDIT: Attempting to print the interval it stops at returns the first component in the matrix, 0,0

You haven’t explained what the purpose is. From what I gather, you are computing the count of the matrix which is what my previous answer is doing.

Also, the += operator does not exist in Lua. I take it that you come from a Python background. The i, v in pairs() loop is the most versatile loop. I omitted the variable for the indices in the two loops on my previous answer, but if you need those you can just name the variables and use them.

The += operator does exist in lua edition 5.1 and will work in roblox lua. Enumeration is actually quite wasteful for a data model like this

Try starting X, and Y iterators on 1 instead of 0.

First thing i tried, even explicitly stating their iteration factor doesnt fix it

My bad, I’m not aware of that.

But for an integer matrix, I don’t think enumeration will be that much more inefficient. Whatever memory is used for enumeration will be quickly garbage collected, if it isn’t utilized.

Can you show a sample of the matrix, so we have an idea of how the table is formatted?

You could have seen it if you downloaded the file but this is how the matrix is constructed

local resolutionX = workspace.CurrentCamera.ViewportSize.X/10
local resolutionY = workspace.CurrentCamera.ViewportSize.Y/10
local pixelMatrix = {}
for x=0, workspace.CurrentCamera.ViewportSize.X,resolutionX do
	pixelMatrix[x] = {}
	for y=0, workspace.CurrentCamera.ViewportSize.Y,resolutionY do
		pixelMatrix[x][y] = Instance.new("Frame")
		pixelMatrix[x][y].Size = UDim2.new(0,1*resolutionX,0,1*resolutionY)
		pixelMatrix[x][y].Position = UDim2.new(0,x, 0, y)
		pixelMatrix[x][y].Name = tostring(x).."-"..tostring(y)
		pixelMatrix[x][y].Parent = script.Parent.Parent.Canvas
		wait()
	end
end

return pixelMatrix

Lua indices start from 1. Your for loop should start from 1 as well on lines 4 and 6

You should make the same change on the code where you’re iterating through the table.

They start from 1 when instancing them. However attempting to index them with stuff below -1 will simply create more logs. It is not a hard cap, it is a DSA, a scaling array (see memory management, the heap in C)

You might also consider removing the yield from your loop, as it’s not infinite it shouldn’t lock up, but you can correct me given your experience with the execution.

If starting from 1 doesn’t solve the issue, start using debug print statements to ensure you know how your matrix is being indexed, field by field.

Already tried that, my computer cant compute that much at once. Also this is not #help-and-feedback:code-review

That is a horrible method of debugging btw, just use watches after all (already tried)

I’ll take a look at your .rbxm real quick. :smiley:

1 Like

So the reason it’s stopping on the first index is because while the matrix is using arrays, the array is indexed with floating points numbers rather than solely integers.

The Lua handbook stipulates that arrays cannot iterate through anything except integers in an array.

To solve this, you would need to index your matrix using a different method, though I’m not sure how you might do so without ruining your intended purpose of the matrix.

You could experiment with rounding, or more costly converting the index to a string, removing the decimal, and converting it back into a number…potentially allowing the array to iterate, though at the cost of a large array.

I’ll try it out after my class

I wish you luck, it’s an interestingly complex problem.

In the module I also returned the resolution on X and Y

local resolutionX = workspace.CurrentCamera.ViewportSize.X/10
local resolutionY = workspace.CurrentCamera.ViewportSize.Y/10
local pixelMatrix = {}
for x=0, workspace.CurrentCamera.ViewportSize.X,resolutionX do
	pixelMatrix[x] = {}
	for y=0, workspace.CurrentCamera.ViewportSize.Y,resolutionY do
		pixelMatrix[x][y] = Instance.new("Frame")
		pixelMatrix[x][y].Size = UDim2.new(0,1*resolutionX,0,1*resolutionY)
		pixelMatrix[x][y].Position = UDim2.new(0,x, 0, y)
		pixelMatrix[x][y].Name = tostring(x).."-"..tostring(y)
		pixelMatrix[x][y].Parent = script.Parent.Parent.Canvas
		wait()
	end
end

return {pixelMatrix, resolutionX, resolutionY}

Here I simply loop through the viewport size with your given resolution on both axis

local pixelMatrix, resX, resY = unpack(require(script.Parent.Modules.Setup))

local count = 0

for x=0,workspace.CurrentCamera.ViewportSize.X, resX do
	for y=0, workspace.CurrentCamera.ViewportSize.Y, resY do
		count += 1
	end
end

print(count)

Hope this helped at all.

Can’t you just floor the resolutionX and resolutionY values down? It’s becoming a floating point value because you’re dividing by an awkward value, I would just floor it down - and try to use power of 2 resolutions…