如何将矩阵的索引映射到一维数组 (C++)? [英] How to map the indexes of a matrix to a 1-dimensional array (C++)?

查看:27
本文介绍了如何将矩阵的索引映射到一维数组 (C++)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 8x8 矩阵,如下所示:

I have an 8x8 matrix, like this:

char matrix[8][8];

此外,我有一个包含 64 个元素的数组,如下所示:

Also, I have an array of 64 elements, like this:

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.

如果我有,比如说,索引 3(列)和 4(行)到矩阵中,我知道它对应于数组中位置 35 的元素,正如我在表中看到的那样画.我相信有某种公式可以将矩阵的 2 个索引转换为数组的单个索引,但我不知道它是什么.

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.

有什么想法吗?

推荐答案

大多数语言存储多维数组的方式是进行如下转换:

The way most languages store multi-dimensional arrays is by doing a conversion like the following:

如果 matrix 的大小为 n(行)乘 m(列),并且我们使用的是行优先顺序"(我们首先沿行数)然后:

If matrix has size, n (rows) by m (columns), and we're using "row-major ordering" (where we count along the rows first) then:

matrix[ i ][ j ] = array[ i*m + j ].

这里 i 从 0 到 (n-1) 和 j 从 0 到 (m-1).

Here i goes from 0 to (n-1) and j from 0 to (m-1).

所以它就像一个以m"为基数的数字系统.请注意,最后一个维度的大小(此处为行数)无关紧要.

So it's just like a number system of base 'm'. Note that the size of the last dimension (here the number of rows) doesn't matter.

为了概念上的理解,请考虑一个 (3x5) 矩阵,其中 'i' 为行号,'j' 为列号.如果您从 i,j = (0,0) --> 开始编号0.对于'row-major' 排序(像这样),布局看起来像:

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:

           |-------- 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 

当您沿行移动(即增加列数)时,您只是开始计数,因此数组索引为 0,1,2....当您到达第二行时,您已经有 5 条目,因此您从索引 1*5 + 0,1,2... 开始.在第三行,您已经有 2*5 条目,因此索引为 2*5 + 0,1,2....

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....

对于更高维度,这个想法可以概括,即对于 3D matrix L by N by M:

For higher dimension, this idea generalizes, i.e. for a 3D matrix L by N by M:

matrix[ i ][ j ][ k ] = array[ i*(N*M) + j*M + k ]

等等.

对于一个非常好的解释,请参阅:http://www.cplusplus.com/doc/tutorial/arrays/; 或更多技术方面的信息:http://en.wikipedia.org/wiki/Row-major_order

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

这篇关于如何将矩阵的索引映射到一维数组 (C++)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆