【问题标题】:Javascript: update a column in matrixJavascript:更新矩阵中的列
【发布时间】:2020-06-08 07:05:33
【问题描述】:

我想更新矩阵的整个列。

我首先初始化矩阵,如:

    m =({length:10}, (_,i) => Array.from({length:2}, (_,j) => i+'x'+j));

然后,我有一个包含 2 个元素(例如 a2)的数组,我想通过操作将其放入矩阵的第 j 列。 如果我使用 matlab,我会执行以下操作:m(:,j)=a2+ m(:,j)

如何在 javascript 中做到这一点?

【问题讨论】:

  • 向我们展示您的尝试。

标签: javascript arrays matrix


【解决方案1】:

你的意思是这样的吗?:

const m = Array.from({length:10}, (_,i) => Array.from({length:2}, (_,j) => i+'x'+j));

console.log("before", m);

const j = 2;
const a2 = "z"
const n = m.map(row => {row.splice(j, 0, a2); return row});

console.log("after", n);

操作前:

before [
  [    "0x0",    "0x1"  ],
  [    "1x0",    "1x1"  ],
  [    "2x0",    "2x1"  ],
  [    "3x0",    "3x1"  ],
  [    "4x0",    "4x1"  ],
  [    "5x0",    "5x1"  ],
  [    "6x0",    "6x1"  ],
  [    "7x0",    "7x1"  ],
  [    "8x0",    "8x1"  ],
  [    "9x0",    "9x1"  ]
]

在操作之后(即在第 2 列添加“z”):

after [
  [    "0x0",    "0x1",    "z"  ],
  [    "1x0",    "1x1",    "z"  ],
  [    "2x0",    "2x1",    "z"  ],
  [    "3x0",    "3x1",    "z"  ],
  [    "4x0",    "4x1",    "z"  ],
  [    "5x0",    "5x1",    "z"  ],
  [    "6x0",    "6x1",    "z"  ],
  [    "7x0",    "7x1",    "z"  ],
  [    "8x0",    "8x1",    "z"  ],
  [    "9x0",    "9x1",    "z"  ]
]

或者如果a2 本身应该是一个数组?然后,它是一维数组的简单情况:

const m = Array.from({length:10}, (_,i) => Array.from({length:2}, (_,j) => i+'x'+j));

console.log("before", m);

const j = 2;
const a2 = Array.from({length:10}, (_,j) => 'z'+j)
const n = m.map((row, i) => {row.splice(j, 0, a2[i]); return row});

console.log("after", n);

操作后(即在第 2 列添加“z{ i }”):

after [
  [    "0x0",    "0x1",    "z0"  ],
  [    "1x0",    "1x1",    "z1"  ],
  [    "2x0",    "2x1",    "z2"  ],
  [    "3x0",    "3x1",    "z3"  ],
  [    "4x0",    "4x1",    "z4"  ],
  [    "5x0",    "5x1",    "z5"  ],
  [    "6x0",    "6x1",    "z6"  ],
  [    "7x0",    "7x1",    "z7"  ],
  [    "8x0",    "8x1",    "z8"  ],
  [    "9x0",    "9x1",    "z9"  ]
]

或者当a2是一个二维数组时更复杂的情况:

const m = Array.from({length:10}, (_,i) => Array.from({length:2}, (_,j) => i+'x'+j));

console.log("before", m);

const j = 2;
const a2 = Array.from({length:10}, (_,i) => Array.from({length:2}, (_,j) => i+'z'+j));
const n = m.map((row, i) => {row.splice(j, 0, a2[i]); return row.flat()});

console.log("after", n);

操作后(即在第 2 列添加“z{ i }”):

after [
  [    "0x0",    "0x1",    "0z0",    "0z1"  ],
  [    "1x0",    "1x1",    "1z0",    "1z1"  ],
  [    "2x0",    "2x1",    "2z0",    "2z1"  ],
  [    "3x0",    "3x1",    "3z0",    "3z1"  ],
  [    "4x0",    "4x1",    "4z0",    "4z1"  ],
  [    "5x0",    "5x1",    "5z0",    "5z1"  ],
  [    "6x0",    "6x1",    "6z0",    "6z1"  ],
  [    "7x0",    "7x1",    "7z0",    "7z1"  ],
  [    "8x0",    "8x1",    "8z0",    "8z1"  ],
  [    "9x0",    "9x1",    "9z0",    "9z1"  ]
]

【讨论】:

    【解决方案2】:

    我认为使用Array.prototype.forEachArray.prototype.splice 是实现此目的的好方法:

    // Setup for demo
    const Row = (w) =>Array.from({length:w}).fill('').map((c,i)=>c=i);
    const Matrix = (h, w) => Array.from({length:h}).fill('').map(r=>r=Row(w));
    const print = (mtx) => mtx.forEach(r=>console.log([...r].join(", ")));
    
    // Original state of matrix
    const m1 = Matrix(2,10);
    print(m1);
    console.log('----------');
    
    // Insertion function
    const insertCol = (mtx, cInd, newCol) => {
      mtx.forEach( (row, rInd) => { row.splice(cInd, 0, newCol[rInd]); }
      );
    }
    
    // Matrix with inserted column
    const someCol = ['X','Y'];
    insertCol(m1, 3, someCol);
    print(m1);

    【讨论】:

      猜你喜欢
      • 2015-11-06
      • 2022-01-03
      • 2015-09-14
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多