You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript矩阵指定索引行操作异常问题排查

Fixing Your Matrix Row/Column Increment Issue

Let's walk through the key bugs in your code that are causing all rows to update instead of just the specified ones, plus fix the broken column operation logic:

1. The Root Cause of All Rows Updating: Shared Array References

Your matrix initialization line is the biggest problem here:

let matrix = new Array(r).fill(new Array(c).fill(0));

When you use fill(new Array(c).fill(0)), you're creating one single column array instance and filling every row of your matrix with a reference to that same array. That means any change you make to any row will show up in all rows—they’re all pointing to the exact same underlying data.

Fix for Initialization

Replace that line with code that creates a fresh, independent array for each row. Two simple ways to do this:

// Option 1: Clean and concise with Array.from
let matrix = Array.from({ length: r }, () => new Array(c).fill(0));

// Option 2: Explicit loop for clarity
let matrix = [];
for (let i = 0; i < r; i++) {
  matrix.push(new Array(c).fill(0));
}

Both approaches ensure each row is its own separate array, so modifying one row won’t affect the others.

2. Broken Column Operation Logic

Your current column update code is targeting a row instead of a column:

matrix[cIdx].map((cv, idx, array) => (cIdx === idx ? array[idx]++ : null));

matrix[cIdx] refers to the row at index cIdx, not the column you want to update. To increment the correct column, you need to iterate over every row and increment the element at position cIdx in each row.

Also, using map here is unnecessary—map is designed to return a new array, not perform in-place modifications. forEach is a much better fit for this kind of side-effect operation.

Fix for Column Updates

Replace the column modification code with:

// Increment every row's element at column cIdx
matrix.forEach(row => row[cIdx]++);

3. Minor: Unnecessary Use of map for Row Updates

While your row update logic works (once the initialization is fixed), using map here is also not ideal. Switch to forEach for clearer intent:

// Increment every element in the target row
matrix[rIdx].forEach((_, idx, array) => array[idx]++);

Full Corrected Code

Putting it all together, here's the fixed function:

const solvematrix = (r, c, indices) => {
  // Create matrix with independent rows
  let matrix = Array.from({ length: r }, () => new Array(c).fill(0));
  
  for (let i = 0; i < indices.length; i++) {
    const rIdx = indices[i][0];
    const cIdx = indices[i][1];
    
    // Modify target row in-place
    matrix[rIdx].forEach((_, idx, array) => array[idx]++);
    
    // Modify target column in-place
    matrix.forEach(row => row[cIdx]++);
  }
  
  return matrix;
};

Testing the Fixed Code

When you call solvematrix(2, 3, [[0, 1],[1, 1]]), you’ll get the expected output:

[[1, 3, 1], [1, 3, 1]]

Let’s confirm the flow:

  • First index pair [0,1]:
    • Increment row 0 → [1,1,1]
    • Increment column 1 → matrix becomes [[1,2,1], [0,1,0]]
  • Second index pair [1,1]:
    • Increment row 1 → [1,2,1]
    • Increment column 1 → final matrix matches the expected result.

内容的提问来源于stack exchange,提问作者stargiraffe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:13:14