如何根据元素列表以编程方式形成矩阵线?
How can I form matrix lines programmaticaly based on an element list?
我正在通过一个朋友与我分享的挑战来练习一些编程逻辑。问题来了:
Write a script that takes an integer n as the numbers of rows and columns of a matrix and take the element list of the matrix.
Example: n=3
matrix = [[11 2 4],
[4 5 6],
[10 8 -12] ]
And print the determinant of the matrix to -100 <= m[i][j] <= 100
到目前为止我的解决方案、代码、问题、疑问:
解决方案
创建一个有n个空位置的数组,以后这些位置存放其他数组,就是矩阵的每一行。
遍历每个空位置以填充新的 matrixLine
在循环内创建一个变量,它将是一个包含元素的矩阵行
循环元素列表以获取一行的必要元素
将矩阵行与元素放在最终矩阵上
代码
function createMatrix(rowsAndColumns, elementsList) {
const finalMatrix = Array(rowsAndColumns)
for(let i = 0; i < finalMatrix.length; i++) {
let matrixLineWithElements = []
for(let k = 0; k < rowsAndColumns; k++) {
matrixLineWithElements[k] = elementsList[k]
}
finalMatrix[i] = matrixLineWithElements
}
}
问题
正如您可能已经在代码中注意到的那样,我从列表中提取相同的元素来形成一行。最终矩阵成功形成,但我不知道如何计算每行的不同值,这是我的终端的图像:
问题
我不再试图弄清楚 while 代码并记下了以下笔记 -
如果我有一个包含四个元素的列表:
[10. 90, 100, 25]
我会形成一个2X2的矩阵,所以我需要统计列表的元素。
0(零)和2是计数的起点,计数盐为2(n变量的值相同)。
这里有某种模式,看,如果我有一个9元素的列表,比如
[10, 90, 100, 25, 36, 48, 2, 5, 6]
我要组成一个3X3的矩阵,所以我需要统计list的元素
0(零)、3、6为计数起点,计数盐为3(n变量值相同)。
如何以编程方式进行计数?
我对这个问题的方法和思考是否有意义,或者有更好的方法吗?
让我们举个例子
Col 0
Col 1
Row 0
0
1
Row 1
2
3
Col 0
Col 1
Col 2
Row 0
0
1
2
Row 1
3
4
5
Row 2
6
7
8
这些是 2 个“矩阵”,我用要放置在其中的 elementsList
元素的索引填充单元格。
您可以看到,对于“第 0”行,列号是索引,但对于第 1 行,它是列索引增加了第一行中的元素数。
在第 2 行中,列索引增加了行长度的 2 倍(如果我们使用第二个版本的代码 - 带有计数器,则增加了已经放置的元素数)。
我们可以推断第 n 行索引从 n*(行的长度)开始并像列索引一样增加 1 因此矩阵单元格中 elementsList
索引的公式:(row number) * (length of row) + columnn index
function createMatrix(rowsAndColumns, elementsList) {
const finalMatrix = Array(rowsAndColumns)
for(let i = 0; i < finalMatrix.length; i++) {
let matrixLineWithElements = []
for(let k = 0; k < rowsAndColumns; k++) {
matrixLineWithElements[k] = elementsList[k+i*finalMatrix.length] // here you just needed to add i times (max i)
}
finalMatrix[i] = matrixLineWithElements
}
return finalMatrix // I added this only so that it can be printed
}
console.log(createMatrix(2,[1,2,3,4]))
其他选项:
function createMatrix(rowsAndColumns, elementsList) {
const finalMatrix = Array(rowsAndColumns)
let counter = 0 // Added counter
for(let i = 0; i < finalMatrix.length; i++) {
let matrixLineWithElements = []
for(let k = 0; k < rowsAndColumns; k++) {
matrixLineWithElements[k] = elementsList[counter]
counter+=1 // Increment counter
}
finalMatrix[i] = matrixLineWithElements
}
return finalMatrix // I added this only so that it can be printed
}
console.log(createMatrix(2,[1,2,3,4]))
但是,我不喜欢改变变量
function createMatrix(n, elementsList) {
const range = (length) => [...Array(length).keys()]
const finalMatrix = range(n).map((i) =>
range(n).map((j) =>
elementsList[i * n + j]
))
return finalMatrix // I added this only so that it can be printed
}
console.log(createMatrix(2,[1,2,3,4]))
我正在通过一个朋友与我分享的挑战来练习一些编程逻辑。问题来了:
Write a script that takes an integer n as the numbers of rows and columns of a matrix and take the element list of the matrix.
Example: n=3 matrix = [[11 2 4], [4 5 6], [10 8 -12] ]
And print the determinant of the matrix to -100 <= m[i][j] <= 100
到目前为止我的解决方案、代码、问题、疑问:
解决方案
创建一个有n个空位置的数组,以后这些位置存放其他数组,就是矩阵的每一行。
遍历每个空位置以填充新的 matrixLine
在循环内创建一个变量,它将是一个包含元素的矩阵行
循环元素列表以获取一行的必要元素
将矩阵行与元素放在最终矩阵上
代码
function createMatrix(rowsAndColumns, elementsList) {
const finalMatrix = Array(rowsAndColumns)
for(let i = 0; i < finalMatrix.length; i++) {
let matrixLineWithElements = []
for(let k = 0; k < rowsAndColumns; k++) {
matrixLineWithElements[k] = elementsList[k]
}
finalMatrix[i] = matrixLineWithElements
}
}
问题
正如您可能已经在代码中注意到的那样,我从列表中提取相同的元素来形成一行。最终矩阵成功形成,但我不知道如何计算每行的不同值,这是我的终端的图像:
问题
我不再试图弄清楚 while 代码并记下了以下笔记 -
如果我有一个包含四个元素的列表:
[10. 90, 100, 25]
我会形成一个2X2的矩阵,所以我需要统计列表的元素。
0(零)和2是计数的起点,计数盐为2(n变量的值相同)。
这里有某种模式,看,如果我有一个9元素的列表,比如
[10, 90, 100, 25, 36, 48, 2, 5, 6]
我要组成一个3X3的矩阵,所以我需要统计list的元素
0(零)、3、6为计数起点,计数盐为3(n变量值相同)。
如何以编程方式进行计数?
我对这个问题的方法和思考是否有意义,或者有更好的方法吗?
让我们举个例子
Col 0 | Col 1 | |
---|---|---|
Row 0 | 0 | 1 |
Row 1 | 2 | 3 |
Col 0 | Col 1 | Col 2 | |
---|---|---|---|
Row 0 | 0 | 1 | 2 |
Row 1 | 3 | 4 | 5 |
Row 2 | 6 | 7 | 8 |
这些是 2 个“矩阵”,我用要放置在其中的 elementsList
元素的索引填充单元格。
您可以看到,对于“第 0”行,列号是索引,但对于第 1 行,它是列索引增加了第一行中的元素数。 在第 2 行中,列索引增加了行长度的 2 倍(如果我们使用第二个版本的代码 - 带有计数器,则增加了已经放置的元素数)。
我们可以推断第 n 行索引从 n*(行的长度)开始并像列索引一样增加 1 因此矩阵单元格中 elementsList
索引的公式:(row number) * (length of row) + columnn index
function createMatrix(rowsAndColumns, elementsList) {
const finalMatrix = Array(rowsAndColumns)
for(let i = 0; i < finalMatrix.length; i++) {
let matrixLineWithElements = []
for(let k = 0; k < rowsAndColumns; k++) {
matrixLineWithElements[k] = elementsList[k+i*finalMatrix.length] // here you just needed to add i times (max i)
}
finalMatrix[i] = matrixLineWithElements
}
return finalMatrix // I added this only so that it can be printed
}
console.log(createMatrix(2,[1,2,3,4]))
其他选项:
function createMatrix(rowsAndColumns, elementsList) {
const finalMatrix = Array(rowsAndColumns)
let counter = 0 // Added counter
for(let i = 0; i < finalMatrix.length; i++) {
let matrixLineWithElements = []
for(let k = 0; k < rowsAndColumns; k++) {
matrixLineWithElements[k] = elementsList[counter]
counter+=1 // Increment counter
}
finalMatrix[i] = matrixLineWithElements
}
return finalMatrix // I added this only so that it can be printed
}
console.log(createMatrix(2,[1,2,3,4]))
但是,我不喜欢改变变量
function createMatrix(n, elementsList) {
const range = (length) => [...Array(length).keys()]
const finalMatrix = range(n).map((i) =>
range(n).map((j) =>
elementsList[i * n + j]
))
return finalMatrix // I added this only so that it can be printed
}
console.log(createMatrix(2,[1,2,3,4]))