Not exactly Renoise related but ... i need help or a big kick

I started to write a library for working with 2D matrices, but something is wrong at the very beginning.
And now I need that kick :slight_smile:
Why does matrix “a” not add up to “b”?
Thanks

Matrix2D = {}
function Matrix2D:new(object, cols, rows, value)
	object = object or {}
	setmetatable(object, self)
	self.__index = self
	self.rows = rows or 1
	self.cols = cols or 1
	self.matrix = {}
	for i=1, self.rows do
		self.matrix[i] = {}
		for j=1, self.cols do
			self.matrix[i][j] = value
		end
	end
	return object
end


function Matrix2D:add(array)
	for i=1, self.rows do
		for j=1, self.cols do
			self.matrix[i][j] = self.matrix[i][j] + array.matrix[i][j]
		end
	end
end

function Matrix2D:set_zero_boundary()
	for i=1, self.rows do
		self.matrix[i][1] = 0
		if i == 1 or i == self.rows then
			for j=1, self.cols do
				self.matrix[i][j] = 0
			end
		end
		self.matrix[i][self.cols] = 0
	end
end

function Matrix2D:print()
	for i=1, self.rows do
		print(table.unpack(self.matrix[i]))
	end
end

a = Matrix2D:new(nil, 10, 5, 1)
b = Matrix2D:new(nil, 10, 5, 0.1)
a:add(b)
a:print()

Seems to add “b” + “b”

Sry i’m idiot. this works.

Matrix2D = {}
function Matrix2D:new(cols, rows, value)
	local object = {}
	setmetatable(object, {__index=self})
	object.rows = rows or 1
	object.cols = cols or 1
	object.matrix = {}
	for i=1, object.rows do
		object.matrix[i] = {}
		for j=1, object.cols do
			object.matrix[i][j] = value
		end
	end
	return object
end
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.