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

查看:551
本文介绍了如何矩阵的索引映射到一个1维数组(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:

如果矩阵有大小,正用m [即i从0变到(n-1)和从0至(m-1)],则j

If matrix has size, n by m [i.e. i goes from 0 to (n-1) and j from 0 to (m-1) ], then:

矩阵[i] [j]的数组= [I×M + J]

因此​​,它就像基地'N'的数字系统。注意,最后维的大小没有关系。

So its just like a number system of base 'n'. Note that the size of the last dimension doesn't matter.

有关概念的理解,认为用'我'作为行数,J作为列数(3×5)矩阵。如果从编号 I,J =(0,0) - > 0 。对于行主的顺序(像这样),布局是这样的:

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 矩阵→N个由M:

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

矩阵[i] [j]的[K] =阵列[我*(N * M)+ J * M + K]

等。

有关一个很好的说明,请参见:<一href=\"http://www.cplusplus.com/doc/tutorial/arrays/\">http://www.cplusplus.com/doc/tutorial/arrays/;或一些技术方面的问题:<一href=\"http://en.wikipedia.org/wiki/Row-major_order\">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

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

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