简体   繁体   English

似乎无法弄清楚为什么我的矩阵乘法函数不起作用

[英]Can not seem to figure out why my matrix multiplication function is not working

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];
      }
    }
  }

The function takes two four by four matrixes and outputs an array of length 16. It does not seem to be giving me the correct array.该函数采用两个四乘四矩阵并输出一个长度为 16 的数组。它似乎没有给我正确的数组。

This should do the trick这应该可以解决问题

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;
}

Notice that you need to initialize the output array and your row x col multiplication was wrong;请注意,您需要初始化输出数组,并且您的row x col乘法是错误的; you need:你需要:
a[row][i] * b[i][col]
instead of代替
a[i][row] * b[col][i]
The function should also return the resulting array.该函数还应返回结果数组。
You can give it a try using JSFiddle: https://jsfiddle.net/1fxLpucg/您可以尝试使用 JSFiddle: https ://jsfiddle.net/1fxLpucg/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 似乎无法弄清楚为什么我的 promise 阵列不工作 - Can't seem to figure out why my promise array isn't working 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 似乎无法弄清楚该脚本为何不起作用 - Can't seem to figure out why the script isn't working 我的简单路由无法正常工作,我不知道为什么 - My simple routing is not working and I can't figure out why d3:我的路径似乎在翻倍,我不知道为什么 - d3: My paths seem to be doubling up and I can't figure out why 我的json请求不会触发beforeSend请求。似乎无法找出原因 - My json request will not fire a beforeSend request.. Can't seem to figure out why 我的CPU在Google Cloud上的使用率已达到160%,我似乎不知道为什么吗? - My CPU is reaching 160% on Google Cloud and I can't seem to figure out why? 我不知道我的 JavaScript 函数不起作用 - I can't figure out my JavaScript function is not working 谁能弄清楚为什么我的jquery函数上下滑动无法在我的第一个li标签上正常工作? - Can anyone figure out why my jquery function slide up and down is not working properly on my first li tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM