Skip to content

Instantly share code, notes, and snippets.

@formigone
Created July 20, 2017 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save formigone/7ce762767a879d136c8dbf2d3649d28d to your computer and use it in GitHub Desktop.
Save formigone/7ce762767a879d136c8dbf2d3649d28d to your computer and use it in GitHub Desktop.
Multiply two matrices represented by 2-dimensional arrays
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