Created
July 20, 2017 02:18
-
-
Save formigone/7ce762767a879d136c8dbf2d3649d28d to your computer and use it in GitHub Desktop.
Multiply two matrices represented by 2-dimensional arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mult(a, b){ | |
return a.map((row) => { | |
return b[0].map((col, j) => { | |
return row.reduce((acc, a0, i) => (acc + a0 * b[i][j]), 0); | |
}); | |
}) | |
} | |
var mat0 = [ | |
[1, 2, 3, 4], | |
[5, 6, 7, 8], | |
]; | |
var mat1 = [ | |
[10, 20, 30, 40, 50, 60, 70], | |
[11, 21, 31, 41, 51, 61, 71], | |
[12, 22, 32, 42, 52, 62, 72], | |
[13, 23, 33, 43, 53, 63, 73], | |
]; | |
mult(mat0, mat1); | |
// [ | |
// [120, 220, 320, 420, 520, 620, 720], | |
// [304, 564, 824, 1084, 1344, 1604, 1864], | |
// ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment