我正在尝试编写一个运行矩阵的程序
[[1,2],[3,4]]
并通过函数将元素乘以三倍
例如:
输入
[[1,2],[3,4]]
输出
[[3,6],[9,12]]
最佳答案
一种可能的方法是迭代矩阵的列,而不是迭代这些列的每一行。
const matrix = [
[1, 2],
[3, 4]
]
const output = []
for (const col of matrix)
{
const triples = []
output.push(triples)
for (const row of col)
{
triples.push(row * 3)
}
}
console.log(output)
关于三倍元素的 Javascript 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54674929/