-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_sum.lua
More file actions
45 lines (40 loc) · 795 Bytes
/
matrix_sum.lua
File metadata and controls
45 lines (40 loc) · 795 Bytes
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
39
40
41
42
43
44
45
local function sumMatrices(a, b)
local rows = #a
local cols = #a[1]
local result = {}
for i = 1, rows do
result[i] = {}
for j = 1, cols do
result[i][j] = a[i][j] + b[i][j]
end
end
return result
end
local function printMatrix(m)
for i = 1, #m do
local row = {}
for j = 1, #m[i] do
row[#row + 1] = string.format("%6.2f", m[i][j])
end
print(table.concat(row, " "))
end
end
local matrixA = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
local matrixB = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
}
local matrixC = sumMatrices(matrixA, matrixB)
print("Matrix A")
printMatrix(matrixA)
print("")
print("Matrix B")
printMatrix(matrixB)
print("")
print("A + B")
printMatrix(matrixC)