-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix-addition
More file actions
26 lines (19 loc) · 794 Bytes
/
matrix-addition
File metadata and controls
26 lines (19 loc) · 794 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
function matrixAddition(a, b) {
let res = [];
for (let i = 0; i < a.length; i++) {
res[i] = [];
for (let j = 0; j < b.length; j++) {
res[i][j] = a[i][j] + b[i][j];
}
}
return res;
}
/*
Write a function that accepts two square matrices (N x N two dimensional arrays), and return the sum of the two. Both matrices being passed into the function will be of size N x N (square), containing only integers.
How to sum two matrices:
Take each cell [n][m] from the first matrix, and add it with the same [n][m] cell from the second matrix. This will be cell [n][m] of the solution matrix.
Visualization:
|1 2 3| |2 2 1| |1+2 2+2 3+1| |3 4 4|
|3 2 1| + |3 2 3| = |3+3 2+2 1+3| = |6 4 4|
|1 1 1| |1 1 3| |1+1 1+1 1+3| |2 2 4|
*/