Last active
September 21, 2017 21:06
-
-
Save fernandozamoraj/d22d10fa9853f777c6744ccbf0d12cd8 to your computer and use it in GitHub Desktop.
Multiply Matrices
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 multiplyMatrix(A, B){ | |
console.log("****MULTIPLY****") | |
let aRows = A.length | |
let aCols = A[0].length | |
let bRows = B.length | |
let bCols = B[0].length | |
let C = [] | |
let i = 0 | |
let j = 0 | |
for(i=0;i<aRows;i++){ | |
C.push([]) | |
for(j=0;j<bCols;j++){ | |
C[i].push(0) | |
} | |
} | |
console.log(C) | |
for(a=0;a<bCols;a++){ | |
for(i = 0; i < aRows; i++){ | |
let sum = 0 | |
for(j = 0; j < aCols; j++){ | |
sum += (A[i][j] * B[j][a]); | |
} | |
C[i][a] = sum; | |
} | |
} | |
return C | |
} | |
function displayMatrix(matrix){ | |
console.log("*******matrix*******") | |
console.log(matrix) | |
let row = 0, | |
col = 0; | |
for(row = 0; row < matrix.length; row++){ | |
let rowString = '' | |
for(col=0;col < matrix[row].length; col++){ | |
rowString = rowString + matrix[row][col] + ' ' | |
} | |
console.log(rowString) | |
} | |
} | |
var m = [[1,2,3],[4,5,6],[7,8,9]] | |
displayMatrix(m) | |
var A = [[1,2], | |
[3,4]] | |
var B = [[2,0], | |
[1,2]] | |
var C = multiplyMatrix(A, B, C) | |
displayMatrix(C) | |
A = [[1,3,2], | |
[3,2,4]] | |
B = [[2,4], | |
[0,1], | |
[3,5]] | |
var C = multiplyMatrix(A, B, C) | |
displayMatrix(C) | |
A = [[1,2,3], | |
[4,5,6]] | |
B = [[7,8], | |
[9,10], | |
[11,12]] | |
var C = multiplyMatrix(A, B, C) | |
displayMatrix(C) | |
A = [[3,4,2]] | |
B = [[13,9,7,15], | |
[8,7,4,6], | |
[6,4,0,3]] | |
var C = multiplyMatrix(A, B, C) | |
displayMatrix(C) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment