将二维数组映射到一维数组中

时间:2022-12-29 18:49:32

I want to represent a 2D array with a 1D array. A function will pass the two indicies (x,y) and the value to store. These two indicies would represent a single element of a 1D array, and set it accordingly. I know the 1D array needs to have the size of arrayWidth × arrayHeight, but I don't know how to set each element.

我想用一个一维数组来表示一个二维数组。函数将传递两个变量(x,y)和要存储的值。这两个分量表示一个一维数组的单个元素,并相应地设置它。我知道1 d数组大小的需要arrayWidth×arrayHeight,但我不知道如何设置每个元素。

For example, how do I distinguish (2,4,3) from (4,2,3)? I tried setting the array as the x*y, but 2*4 and 4*2 would result in the same spot in the array and I need them to be different.

例如,如何区分(2,4,3)和(4,2,3)?我尝试将数组设置为x*y,但是2*4和4*2会在数组中产生相同的位置,我需要它们不同。

7 个解决方案

#1


116  

You need to decide whether the array elements will be stored in row order or column order and then be consistent about it. http://en.wikipedia.org/wiki/Row-major_order

您需要决定数组元素是按行还是按列存储,然后保持一致。http://en.wikipedia.org/wiki/Row-major_order

The C language uses row order for Multidimensional arrays

C语言对多维数组使用行顺序

To simulate this with a single dimensional array, you multiply the row index by the width, and add the column index thus:

要用一个单维数组来模拟这一点,可以将行索引与宽度相乘,然后添加列索引如下:

 int array[width * height]; int SetElement(int row, int col, int value) {    array[width * row + col] = value;   }

#2


15  

Example : we want to represent an 2D array of SIZE_X and SIZE_Y size. That means that we will have MAXY consecutive rows of MAXX size. Hence the set function is

我们想要表示一个二维数组的SIZE_X和SIZE_Y大小。这意味着我们将有MAXX大小的MAXY连续行。因此集合函数是

void set_array( int x, int y, int val ) { array[ x * SIZE_Y + y ] = val; }

The get would be:

得到的是:

int get_array( int x, int y ) { return array[ x * SIZE_Y + y ]; }

#3


13  

The typical formula for recalculation of 2D array indices into 1D array index is

将二维数组索引重新计算为一维数组索引的典型公式为

index = indexX * arrayWidth + indexY;

Alternatively you can use

或者你可以使用

index = indexY * arrayHeight + indexX;

(assuming that arrayWidth is measured along X axis, and arrayHeight along Y axis)

(假设沿X轴测量arrayWidth,沿Y轴测量arrayHeight)

Of course, one can come up with many different formulae that provide alternative unique mappings, but normally there's no need to.

当然,可以提出许多不同的公式来提供不同的惟一映射,但通常不需要这样做。

In C/C++ languages built-in multidimensional arrays are stored in memory so that the last index changes the fastest, meaning that for an array declared as

在C/ c++语言中,内置的多维数组存储在内存中,以便最后一个索引更改最快,这意味着对于一个声明为的数组来说。

int xy[10][10];

element xy[5][3] is immediately followed by xy[5][4] in memory. You might want to follow that convention as well, choosing one of the above two formulae depending on which index (X or Y) you consider to be the "last" of the two.

元素xy[5][3]在记忆中紧随其后的是xy[5][4]。您可能也想遵循这个约定,根据您认为这两个公式中的哪个索引(X或Y)是“最后”,选择上述两个公式中的一个。

#4


6  

As other have said C maps in row order

正如其他人所说,C映射是按行排列的

   #include <stdio.h>   int main(int argc, char **argv) {   int i, j, k;   int arr[5][3];   int *arr2 = (int*)arr;       for (k=0; k<15; k++) {          arr2[k] = k;          printf("arr[%d] = %2d\n", k, arr2[k]);       }       for (i=0; i<5; i++) {         for (j=0; j< 3; j++) {            printf("arr2[%d][%d] = %2d\n", i, j ,arr[i][j]);         }       }     } 

Output:

输出:

arr[0] =  0arr[1] =  1arr[2] =  2arr[3] =  3arr[4] =  4arr[5] =  5arr[6] =  6arr[7] =  7arr[8] =  8arr[9] =  9arr[10] = 10arr[11] = 11arr[12] = 12arr[13] = 13arr[14] = 14arr2[0][0] =  0arr2[0][1] =  1arr2[0][2] =  2arr2[1][0] =  3arr2[1][1] =  4arr2[1][2] =  5arr2[2][0] =  6arr2[2][1] =  7arr2[2][2] =  8arr2[3][0] =  9arr2[3][1] = 10arr2[3][2] = 11arr2[4][0] = 12arr2[4][1] = 13arr2[4][2] = 14

#5


1  

using row major example:

使用行主要的例子:

A(i,j) = a[i + j*ld]; // where ld is the leading dimension                      // (commonly same as array dimension in i)// matrix like notation using preprocessor hack, allows to hide indexing#define A(i,j) A[(i) + (j)*ld]double *A = ...;size_t ld = ...;A(i,j) = ...;... = A(j,i);

#6


1  

It's important to store the data in a way that it can be retrieved in the languages used. C-language stores in row-major order (all of first row comes first, then all of second row,...) with every index running from 0 to it's dimension-1. So the order of array x[2][3] is x[0][0], x[0][1], x[0][2], x[1][0], x[1][1], x[1][2]. So in C language, x[i][j] is stored the same place as a 1-dimensional array entry x1dim[ i*3 +j]. If the data is stored that way, it is easy to retrieve in C language.

以一种可以用所用语言检索的方式存储数据是很重要的。c语言以行为主的顺序存储(第一行都是第一行,然后是第二行,…),每个索引都从0运行到它的维度1。数组的顺序x[2][3]是x[0][0],x[0][1],x[0][2],x[1][0],x[1][1],[1][2]。因此,在C语言中,x[i][j]与一维数组条目x1dim[i*3 +j]存储在同一个位置。如果数据以这种方式存储,那么很容易用C语言检索。

Fortran and MATLAB are different. They store in column-major order (all of first column comes first, then all of second row,...) and every index runs from 1 to it's dimension. So the index order is the reverse of C and all the indices are 1 greater. If you store the data in the C language order, FORTRAN can find X_C_language[i][j] using X_FORTRAN(j+1, i+1). For instance, X_C_language[1][2] is equal to X_FORTRAN(3,2). In 1-dimensional arrays, that data value is at X1dim_C_language[2*Cdim2 + 3], which is the same position as X1dim_FORTRAN(2*Fdim1 + 3 + 1). Remember that Cdim2 = Fdim1 because the order of indices is reversed.

Fortran和MATLAB是不同的。它们以列-主顺序存储(第一列都是第一列,然后是第二行,…),每个索引都从1运行到它的维度。所以指数顺序是C的逆,所有的指数都是1。如果以C语言顺序存储数据,FORTRAN可以使用X_FORTRAN(j+1, i+1)找到X_C_language[i][j] [j]。例如,X_C_language[1][2]等于X_FORTRAN(3,2)。在一维数组中,该数据值位于X1dim_C_language[2*Cdim2 + 3],与X1dim_FORTRAN(2*Fdim1 + 3 + 1)的位置相同。

MATLAB is the same as FORTRAN. Ada is the same as C except the indices normally start at 1. Any language will have the indices in one of those C or FORTRAN orders and the indices will start at 0 or 1 and can be adjusted accordingly to get at the stored data.

MATLAB与FORTRAN相同。Ada和C是一样的,除了通常从1开始的指数。任何语言都将索引放在其中一个C或FORTRAN命令中,索引将从0或1开始,并可以相应地调整以获取存储的数据。

Sorry if this explanation is confusing, but I think it is accurate and important for a programmer to know.

对不起,如果这个解释让人困惑,但我认为它是准确和重要的程序员知道。

#7


-2  

You should be able to access the 2d array with a simple pointer in place. The array[x][y] will be arranged in the pointer as p[0x * width + 0y][0x * width + 1y]...[0x * width + n-1y][1x * width + 0y] etc.

您应该能够使用一个简单的指针访问2d数组。数组[x][y]将在指针中排列为p[0x * width + 0y][0x * width + 1y]…[0x *宽度+ n-1y][1x *宽度+ 0y]等。

#1


116  

You need to decide whether the array elements will be stored in row order or column order and then be consistent about it. http://en.wikipedia.org/wiki/Row-major_order

您需要决定数组元素是按行还是按列存储,然后保持一致。http://en.wikipedia.org/wiki/Row-major_order

The C language uses row order for Multidimensional arrays

C语言对多维数组使用行顺序

To simulate this with a single dimensional array, you multiply the row index by the width, and add the column index thus:

要用一个单维数组来模拟这一点,可以将行索引与宽度相乘,然后添加列索引如下:

 int array[width * height]; int SetElement(int row, int col, int value) {    array[width * row + col] = value;   }

#2


15  

Example : we want to represent an 2D array of SIZE_X and SIZE_Y size. That means that we will have MAXY consecutive rows of MAXX size. Hence the set function is

我们想要表示一个二维数组的SIZE_X和SIZE_Y大小。这意味着我们将有MAXX大小的MAXY连续行。因此集合函数是

void set_array( int x, int y, int val ) { array[ x * SIZE_Y + y ] = val; }

The get would be:

得到的是:

int get_array( int x, int y ) { return array[ x * SIZE_Y + y ]; }

#3


13  

The typical formula for recalculation of 2D array indices into 1D array index is

将二维数组索引重新计算为一维数组索引的典型公式为

index = indexX * arrayWidth + indexY;

Alternatively you can use

或者你可以使用

index = indexY * arrayHeight + indexX;

(assuming that arrayWidth is measured along X axis, and arrayHeight along Y axis)

(假设沿X轴测量arrayWidth,沿Y轴测量arrayHeight)

Of course, one can come up with many different formulae that provide alternative unique mappings, but normally there's no need to.

当然,可以提出许多不同的公式来提供不同的惟一映射,但通常不需要这样做。

In C/C++ languages built-in multidimensional arrays are stored in memory so that the last index changes the fastest, meaning that for an array declared as

在C/ c++语言中,内置的多维数组存储在内存中,以便最后一个索引更改最快,这意味着对于一个声明为的数组来说。

int xy[10][10];

element xy[5][3] is immediately followed by xy[5][4] in memory. You might want to follow that convention as well, choosing one of the above two formulae depending on which index (X or Y) you consider to be the "last" of the two.

元素xy[5][3]在记忆中紧随其后的是xy[5][4]。您可能也想遵循这个约定,根据您认为这两个公式中的哪个索引(X或Y)是“最后”,选择上述两个公式中的一个。

#4


6  

As other have said C maps in row order

正如其他人所说,C映射是按行排列的

   #include <stdio.h>   int main(int argc, char **argv) {   int i, j, k;   int arr[5][3];   int *arr2 = (int*)arr;       for (k=0; k<15; k++) {          arr2[k] = k;          printf("arr[%d] = %2d\n", k, arr2[k]);       }       for (i=0; i<5; i++) {         for (j=0; j< 3; j++) {            printf("arr2[%d][%d] = %2d\n", i, j ,arr[i][j]);         }       }     } 

Output:

输出:

arr[0] =  0arr[1] =  1arr[2] =  2arr[3] =  3arr[4] =  4arr[5] =  5arr[6] =  6arr[7] =  7arr[8] =  8arr[9] =  9arr[10] = 10arr[11] = 11arr[12] = 12arr[13] = 13arr[14] = 14arr2[0][0] =  0arr2[0][1] =  1arr2[0][2] =  2arr2[1][0] =  3arr2[1][1] =  4arr2[1][2] =  5arr2[2][0] =  6arr2[2][1] =  7arr2[2][2] =  8arr2[3][0] =  9arr2[3][1] = 10arr2[3][2] = 11arr2[4][0] = 12arr2[4][1] = 13arr2[4][2] = 14

#5


1  

using row major example:

使用行主要的例子:

A(i,j) = a[i + j*ld]; // where ld is the leading dimension                      // (commonly same as array dimension in i)// matrix like notation using preprocessor hack, allows to hide indexing#define A(i,j) A[(i) + (j)*ld]double *A = ...;size_t ld = ...;A(i,j) = ...;... = A(j,i);

#6


1  

It's important to store the data in a way that it can be retrieved in the languages used. C-language stores in row-major order (all of first row comes first, then all of second row,...) with every index running from 0 to it's dimension-1. So the order of array x[2][3] is x[0][0], x[0][1], x[0][2], x[1][0], x[1][1], x[1][2]. So in C language, x[i][j] is stored the same place as a 1-dimensional array entry x1dim[ i*3 +j]. If the data is stored that way, it is easy to retrieve in C language.

以一种可以用所用语言检索的方式存储数据是很重要的。c语言以行为主的顺序存储(第一行都是第一行,然后是第二行,…),每个索引都从0运行到它的维度1。数组的顺序x[2][3]是x[0][0],x[0][1],x[0][2],x[1][0],x[1][1],[1][2]。因此,在C语言中,x[i][j]与一维数组条目x1dim[i*3 +j]存储在同一个位置。如果数据以这种方式存储,那么很容易用C语言检索。

Fortran and MATLAB are different. They store in column-major order (all of first column comes first, then all of second row,...) and every index runs from 1 to it's dimension. So the index order is the reverse of C and all the indices are 1 greater. If you store the data in the C language order, FORTRAN can find X_C_language[i][j] using X_FORTRAN(j+1, i+1). For instance, X_C_language[1][2] is equal to X_FORTRAN(3,2). In 1-dimensional arrays, that data value is at X1dim_C_language[2*Cdim2 + 3], which is the same position as X1dim_FORTRAN(2*Fdim1 + 3 + 1). Remember that Cdim2 = Fdim1 because the order of indices is reversed.

Fortran和MATLAB是不同的。它们以列-主顺序存储(第一列都是第一列,然后是第二行,…),每个索引都从1运行到它的维度。所以指数顺序是C的逆,所有的指数都是1。如果以C语言顺序存储数据,FORTRAN可以使用X_FORTRAN(j+1, i+1)找到X_C_language[i][j] [j]。例如,X_C_language[1][2]等于X_FORTRAN(3,2)。在一维数组中,该数据值位于X1dim_C_language[2*Cdim2 + 3],与X1dim_FORTRAN(2*Fdim1 + 3 + 1)的位置相同。

MATLAB is the same as FORTRAN. Ada is the same as C except the indices normally start at 1. Any language will have the indices in one of those C or FORTRAN orders and the indices will start at 0 or 1 and can be adjusted accordingly to get at the stored data.

MATLAB与FORTRAN相同。Ada和C是一样的,除了通常从1开始的指数。任何语言都将索引放在其中一个C或FORTRAN命令中,索引将从0或1开始,并可以相应地调整以获取存储的数据。

Sorry if this explanation is confusing, but I think it is accurate and important for a programmer to know.

对不起,如果这个解释让人困惑,但我认为它是准确和重要的程序员知道。

#7


-2  

You should be able to access the 2d array with a simple pointer in place. The array[x][y] will be arranged in the pointer as p[0x * width + 0y][0x * width + 1y]...[0x * width + n-1y][1x * width + 0y] etc.

您应该能够使用一个简单的指针访问2d数组。数组[x][y]将在指针中排列为p[0x * width + 0y][0x * width + 1y]…[0x *宽度+ n-1y][1x *宽度+ 0y]等。