未定义嵌套循环 (Javascript)

Nested Loop not defined (Javascript)

我的结果矩阵似乎未定义。在第 25 行,这是我的 chrome 控制台给出的错误:"Cannot set property "0" of undefined."

在查看类似问题时,我看到的大多数矩阵乘法解决方案都使用 3 个嵌套循环,而不是我的 4 个。这些可能是更好的方法,但四个循环是唯一有意义的方法我,因为迭代是在两个单独的行和两个单独的列上进行的。如果这是我的错误问题的原因,有人可以解释为什么吗?

const A = [ [-4,0,5], [-3,-1,2], [6,7,-2], [1, 1, 2]],B = [ [1, 0, 3, 0], [4,5,-1, 2], [2, 4, 3, 1]],C = [];
for (var i = 0; i < A.length; i++) {
 //C[i] = 0;
 for (var j = 0; j < A[j].length; j++) {
  //console.log(A[i][j]);
  for (var y = 0; y < B[0].length; y++) {
   C[i][y] = 0;
   for (var x = 0; x < B.length; x++) {
    //console.log(B[x][y]+ "["+x+","+y+"]");
    console.log(C[i][y] + "[" + i + "," + y);
    C[i][y] += A[i][j] * B[x][y];
   }
   console.log(C[i][y] + "[" + i + "," + y + "] is the resultant matrix");
  }
 }
}        

//C[i] = 0; 更改为 C[i] = [];。您需要在 C[i] 下初始化数组以便稍后访问它 C[i][y] = 0;

const A = [ [-4,0,5], [-3,-1,2], [6,7,-2], [1, 1, 2]],B = [ [1, 0, 3, 0], [4,5,-1, 2], [2, 4, 3, 1]],C = [];
for (var i = 0; i < A.length; i++) {
 C[i] = [];
 for (var j = 0; j < A[j].length; j++) {

  for (var y = 0; y < B[0].length; y++) {
   C[i][y] = 0;
   for (var x = 0; x < B.length; x++) {
    C[i][y] += A[i][j] * B[x][y];
   }
  }
 }
}
console.log(C);

const A = [ [-4,0,5], [-3,-1,2], [6,7,-2], [1, 1, 2]],B = [ [1, 0, 3, 0], [4,5,-1, 2], [2, 4, 3, 1]],C = [];
for (var i = 0; i < A.length; i++) {
 C[i] = [];
 for (var j = 0; j < A[j].length; j++) {

  for (var y = 0; y < B[0].length; y++) {
   C[i][y] = 0;
   for (var x = 0; x < B.length; x++) {
    C[i][y] += A[i][j] * B[x][y];
   }
  }
 }
}
console.log(C);