Javascript 似乎无法理解为什么我的矩阵乘法函数不起作用 let multiply=函数(a,b){ 设c=[]; for(设行=0;行
该函数接受两个四乘四矩阵,并输出一个长度为16的数组。它似乎没有给出正确的数组。这应该可以解决问题Javascript 似乎无法理解为什么我的矩阵乘法函数不起作用 let multiply=函数(a,b){ 设c=[]; for(设行=0;行,javascript,matrix,Javascript,Matrix,该函数接受两个四乘四矩阵,并输出一个长度为16的数组。它似乎没有给出正确的数组。这应该可以解决问题 let multiply = function(a, b) { let c = []; for(let row = 0; row < 4; row++){ for(let col = 0; col < 4;col++){ for(let i = 0; i < 4; i++){ c[col + (4*row)] += a[i][row]
let multiply = function(a, b) {
let c = [];
for(let row = 0; row < 4; row++){
for(let col = 0; col < 4;col++){
for(let i = 0; i < 4; i++){
c[col + (4*row)] += a[i][row] * b[col][i];
}
}
}
let multiply=函数(a,b){
设c=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
for(设行=0;行<4;行++){
for(设col=0;col<4;col++){
for(设i=0;i<4;i++){
c[col+(4*行)]+=a[row][i]*b[i][col];
}
}
}
返回c;
}
请注意,您需要初始化输出数组,并且您的行x列
乘法错误;您需要:a[row][i]*b[i][col]
而不是
a[i][row]*b[col][i]
函数还应返回结果数组。
您可以使用JSFiddle尝试一下:是
常量a=[[1,2,3,4],[1,2,3,4],[1,2,3,4];
是的有效输入吗?如果您的问题得到了回答,请将其标记为。
let multiply = function(a, b) {
let c = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
for(let row = 0; row < 4; row++){
for(let col = 0; col < 4; col++){
for(let i = 0; i < 4; i++){
c[col + (4*row)] += a[row][i] * b[i][col];
}
}
}
return c;
}