如何将矩阵的索引映射到一维数组(C++)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14015556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to map the indexes of a matrix to a 1-dimensional array (C++)?
提问by Fernando Aires Castello
I have an 8x8 matrix, like this:
我有一个 8x8 矩阵,如下所示:
char matrix[8][8];
Also, I have an array of 64 elements, like this:
另外,我有一个包含 64 个元素的数组,如下所示:
char array[64];
Then I have drawn the matrix as a table, and filled the cells with numbers, each number being incremented from left to right, top to bottom.
然后我将矩阵绘制为表格,并用数字填充单元格,每个数字从左到右、从上到下递增。
If I have, say, indexes 3 (column) and 4 (row) into the matrix, I know that it corresponds to the element at position 35 in the array, as it can be seen in the table that I've drawn. I believe there is some sort of formula to translate the 2 indexes of the matrix into a single index of the array, but I can't figure out what it is.
如果我有,比如说,索引 3(列)和 4(行)到矩阵中,我知道它对应于数组中位置 35 的元素,正如在我绘制的表格中看到的那样。我相信有某种公式可以将矩阵的 2 个索引转换为数组的单个索引,但我不知道它是什么。
Any ideas?
有任何想法吗?
回答by DilithiumMatrix
The way most languages store multi-dimensional arrays is by doing a conversion like the following:
大多数语言存储多维数组的方式是进行如下转换:
If matrix
has size, n by m [i.e. i goes from 0 to (n-1) and j from 0 to (m-1) ], then:
如果matrix
有大小,n x m [即 i 从 0 到 (n-1) 和 j 从 0 到 (m-1) ],则:
matrix[ i ][ j ] = array[ i*m + j ]
.
matrix[ i ][ j ] = array[ i*m + j ]
.
So its just like a number system of base 'n'. Note that the size of the last dimension doesn't matter.
所以它就像一个以'n'为基数的数字系统。请注意,最后一个维度的大小无关紧要。
For a conceptual understanding, think of a (3x5) matrix with 'i' as the row number, and 'j' as the column number. If you start numbering from i,j = (0,0) --> 0
. For 'row-major'ordering (like this), the layout looks like:
为了概念上的理解,可以想象一个 (3x5) 矩阵,其中 'i' 为行号,'j' 为列号。如果您从i,j = (0,0) --> 0
. 对于“行优先”排序(像这样),布局如下所示:
|-------- 5 ---------|
Row ______________________ _ _
0 |0 1 2 3 4 | |
1 |5 6 7 8 9 | 3
2 |10 11 12 13 14| _|_
|______________________|
Column 0 1 2 3 4
As you move along the row (i.e. increase the column number), you just start counting up, so the Array indices are 0,1,2...
. When you get to the second row, you already have 5
entries, so you start with indices 1*5 + 0,1,2...
. On the third row, you have 2*5
entries already, thus the indices are 2*5 + 0,1,2...
.
当您沿着行移动(即增加列数)时,您只是开始计数,因此数组索引为0,1,2...
. 当你到达第二行时,你已经有了5
条目,所以你从索引开始1*5 + 0,1,2...
。在第三行,您已经有了2*5
条目,因此索引是2*5 + 0,1,2...
。
For higher dimension, this idea generalizes, i.e. for a 3D matrix
L by N by M:
对于更高维度,这个想法可以概括,即对于 3D matrix
L by N by M:
matrix[ i ][ j ][ k ] = array[ i*(N*M) + j*M + k ]
matrix[ i ][ j ][ k ] = array[ i*(N*M) + j*M + k ]
and so on.
等等。
For a really good explanation, see: http://www.cplusplus.com/doc/tutorial/arrays/; or for some more technical aspects: http://en.wikipedia.org/wiki/Row-major_order
有关非常好的解释,请参阅:http: //www.cplusplus.com/doc/tutorial/arrays/;或更多技术方面:http: //en.wikipedia.org/wiki/Row-major_order
回答by mvelusce
For row-major ordering, I believe the statement matrix[ i ][ j ] = array[ i*n + j ]
is wrong.
对于行优先排序,我认为该语句matrix[ i ][ j ] = array[ i*n + j ]
是错误的。
The offset should be offset = (row * NUMCOLS) + column
.
偏移量应该是offset = (row * NUMCOLS) + column
。
Your statement results to be row * NUMROWS + column
, which is wrong.
你的陈述结果是row * NUMROWS + column
,这是错误的。
The links you provided give a correct explanation.
您提供的链接给出了正确的解释。
回答by Donny Verduijn
Something like this?
像这样的东西?
//columns = amount of columns, x = column, y = row
var calculateIndex = function(columns, x, y){
return y * columns + x;
};
The example below converts an index back to x and y coordinates.
下面的示例将索引转换回 x 和 y 坐标。
//i = index, x = amount of columns, y = amount of rows
var calculateCoordinates = function(index, columns, rows){
//for each row
for(var i=0; i<rows; i++){
//check if the index parameter is in the row
if(index < (columns * i) + columns && index >= columns * i){
//return x, y
return [index - columns * i, i];
}
}
return null;
};