matrix multiplication in javascript code example
Example 1: matrix multiplication js
function multiply(a, b) {
var aNumRows = a.length, aNumCols = a[0].length,
bNumRows = b.length, bNumCols = b[0].length,
m = new Array(aNumRows);
for (var r = 0; r < aNumRows; ++r) {
m[r] = new Array(bNumCols);
for (var c = 0; c < bNumCols; ++c) {
m[r][c] = 0;
for (var i = 0; i < aNumCols; ++i) {
m[r][c] += a[r][i] * b[i][c];
}
}
}
return m;
}
Example 2: matrix multiplication javascrpt
function multiplyMatrix(matrixA, matrixB)
{
var result = new Array();
numColsRows=2;
for (var i = 0; i < numColsRows; i++)
{
for (var j = 0; j < numColsRows; j++)
{
var matrixRow = new Array();
var rrr = new Array();
var resu = new Array();
for (var k = 0; k < numColsRows; k++)
{
rrr.push(parseInt(matrixA[i][k])*parseInt(matrixB[k][j]));
}
resu.push(parseInt(rrr[i])+parseInt(rrr[i+1]));
result.push(resu);
}
}
return result;
}