-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex14.1.lua
38 lines (35 loc) · 829 Bytes
/
ex14.1.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
local function print_matrix(m)
for i = 1, #m do
for c, v in pairs(m[i]) do
io.write(i, c, v)
end
io.write("\n")
end
end
local function add_matrix(a, b)
local c = {}
for row = 1, #a do
local new_row = {}
for column, value in pairs(b[row]) do
new_row[column] = (new_row[column] or 0) + value
if new_row[column] == 0 then
new_row[column] = nil
end
end
for column, value in pairs(a[row]) do
new_row[column] = (new_row[column] or 0) + value
if new_row[column] == 0 then
new_row[column] = nil
end
end
c[row] = new_row
end
return c
end
local a = {{[1] = 1}, {[2] = 1}, {[3] = 1}}
local b = {{[1] = 2}, {[2] = 2}, {[3] = 2}}
local c = add_matrix(a, b)
print_matrix(c)
assert(c[1][1] == 3)
assert(c[2][2] == 3)
assert(c[3][3] == 3)