乘在Javascript 2矩阵 [英] Multiply 2 matrices in Javascript
问题描述
我做了2乘以矩阵的功能。矩阵将始终具有相同的行数和列数。 (2×2,5×5,23x23,...)
当我打印出来,这是行不通的。为什么呢?
例如,如果我创建了两个2×2矩阵:
矩阵matrixA:
[1] [2][3] [4]
matrixB:
[5] [6][7] [8]
结果应该是:
[19] [22][43] [50]
( http://ncalculators.com/matrix/2x2-matrix-multiplication -calculator.htm )
不过,我得到:
[19] [未定义][22] [indefined]
函数multiplyMatrix(矩阵matrixA,matrixB)\r
{\r
VAR的结果=新的Array(); //声明数组\r
\r
// VAR numColsRows = $(#matrixRC)VAL();\r
numColsRows = 2;\r
\r
//通过第一矩阵的行迭代\r
对于(VAR I = 0; I< numColsRows;我++)\r
{\r
//通过第二矩阵列迭代\r
对于(VAR J = 0; J< numColsRows; J ++)\r
{\r
VAR matrixRow =新的Array(); //声明数组\r
VAR RRR =新的Array();\r
VAR地区环境部门一道=新的Array();\r
//计算成对产品的总和\r
对于(VAR K = 0; K< numColsRows; k ++)\r
{\r
rrr.push(parseInt函数(矩阵matrixA [I] [K])* parseInt函数(matrixB [K] [J]));\r
} // 3\r
resu.push(parseInt函数(RRR [I])+ parseInt函数(RRR [I + 1]));\r
\r
result.push(地区环境部门一道);\r
//result.push(matrixRow);\r
} // 2\r
} // 1\r
返回结果;\r
} //功能multiplyMatrix
\r
您感到困惑与您的各种临时数组。在未定义
值由下方的内部循环线路输出的越界访问造成的。
我建议你坚持做的乘法结果单个阵列。正如你可能知道,是顺利的JavaScript不允许初始化多维数组。使一个二维阵列,则必须初始化一个一维数组,然后遍历其元素和初始化每一个到一个一维阵列
函数乘(A,B){\r
变种aNumRows =则为a.length,aNumCols =一个[0]。长度,\r
bNumRows = b.length个,bNumCols = B [0]。长度,\r
M =新的Array(aNumRows); //初始化排阵\r
为(变量R = 0;为r aNumRows ++ r)的{\r
M [R] =新阵列(bNumCols); //初始化当前行\r
为(变量C = 0;℃下bNumCols ++三){\r
M [R] [C] = 0; //初始化当前单元格\r
对于(VAR I = 0; I< aNumCols ++我){\r
M [R] [C] + = A [R] [I] * B [I] [C];\r
}\r
}\r
}\r
返回米;\r
}\r
\r
功能显示(M){\r
为(变量R = 0;为r m.length ++ r)的{\r
的document.write('和; NBSP;&安培; NBSP;+ M [R]。加入('')+'< BR />');\r
}\r
}\r
\r
变种一个= [[8,3],[2,4],[3,6],\r
B = [[1,2,3],[4,6,8]];\r
文件撰写('矩阵A:LT; BR />');\r
显示器(a)的\r
文件撰写('矩阵B:LT; BR />');\r
显示(B);\r
文件撰写('A * B =< BR />');\r
显示器(乘法(A,B));
\r
I'm doing a function that multiplies 2 matrices. The matrices will always have the same number of rows and columns. (2x2, 5x5, 23x23, ...)
When I print it, it doesn't work. Why?
For example, if I create two 2x2 matrices:
matrixA:
[1][2]
[3][4]
matrixB:
[5][6]
[7][8]
The result should be:
[19][22]
[43][50]
(http://ncalculators.com/matrix/2x2-matrix-multiplication-calculator.htm)
But, I get:
[19][undefined]
[22][indefined]
function multiplyMatrix(matrixA, matrixB)
{
var result = new Array();//declare an array
//var numColsRows=$("#matrixRC").val();
numColsRows=2;
//iterating through first matrix rows
for (var i = 0; i < numColsRows; i++)
{
//iterating through second matrix columns
for (var j = 0; j < numColsRows; j++)
{
var matrixRow = new Array();//declare an array
var rrr = new Array();
var resu = new Array();
//calculating sum of pairwise products
for (var k = 0; k < numColsRows; k++)
{
rrr.push(parseInt(matrixA[i][k])*parseInt(matrixB[k][j]));
}//for 3
resu.push(parseInt(rrr[i])+parseInt(rrr[i+1]));
result.push(resu);
//result.push(matrixRow);
}//for 2
}//for 1
return result;
}// function multiplyMatrix
You're getting confused with your various temporary arrays. The undefined
values are caused by out-of-bounds access on the line below your innermost loop.
I recommend that you stick to making a single array for the result of the multiplication. As you're probably aware, the hitch is that JavaScript doesn't allow you to initialize a multi-dimensional array. To make a two-dimensional array, you have to initialize a one-dimensional array, then iterate over its elements and initialize each one to a one-dimensional array.
function multiply(a, b) {
var aNumRows = a.length, aNumCols = a[0].length,
bNumRows = b.length, bNumCols = b[0].length,
m = new Array(aNumRows); // initialize array of rows
for (var r = 0; r < aNumRows; ++r) {
m[r] = new Array(bNumCols); // initialize the current row
for (var c = 0; c < bNumCols; ++c) {
m[r][c] = 0; // initialize the current cell
for (var i = 0; i < aNumCols; ++i) {
m[r][c] += a[r][i] * b[i][c];
}
}
}
return m;
}
function display(m) {
for (var r = 0; r < m.length; ++r) {
document.write(' '+m[r].join(' ')+'<br />');
}
}
var a = [[8, 3], [2, 4], [3, 6]],
b = [[1, 2, 3], [4, 6, 8]];
document.write('matrix a:<br />');
display(a);
document.write('matrix b:<br />');
display(b);
document.write('a * b =<br />');
display(multiply(a, b));
这篇关于乘在Javascript 2矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!