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
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”