简体   繁体   English

1D数组到2D数组映射

[英]1D array to 2D array mapping

This may sound like a homework problem, but I swear it isn't. 这可能听起来像是一个家庭作业问题,但我发誓它不是。

I'm trying to build an iterator for this 2D-array wrapper class. 我正在尝试为这个2D数组包装类构建一个迭代器。 I figured that if I am able to solve this problem, then I can build my iterator. 我想如果我能够解决这个问题,那么我可以构建我的迭代器。

I have this 1D array of 9 consecutive integers starting at 0 and ending at 8. 我有这个由9个连续整数组成的1D数组,从0开始到8结束。

[0, 1, 2, 3, 4, 5, 6, 7, 8]

I am given two variables horizontal_size = 3 and vertical_size = 3 给我两个变量horizontal_size = 3vertical_size = 3

I want to make this array into a 2D array that is horizontal_size by vertical_size . 我想把这个数组变成一个2D数组,即vertical_size horizontal_size let's call them h and v for brevity. 为简洁起见,我们称之为hv

The result that I want to generate is this: 我想要生成的结果是:

0 1 2
3 4 5
6 7 8

Given the value in the 1D array, which tells me the index, Also given h and v , which are both 3 in this case. 给出1D数组中的值,它告诉我索引,同样给出hv ,在这种情况下都是3。 Is there a way to generate the indices on the 2D array? 有没有办法在2D阵列上生成索引?

For example, the first element in the 1D array is 0, which maps to array[0][0] . 例如,1D数组中的第一个元素是0,它映射到array[0][0] The second element is 1, which maps to array[0][1] 第二个元素是1,映射到array[0][1]

I figured out that I can get the vertical index by doing array1d[i] mod vertical_size . 我想通过做array1d[i] mod vertical_size我可以得到垂直索引。

           for getting the vertical index ::: th 

0 = [0][0] 0 mod 3 = 0 1 = [0][1] 1 mod 3 = 1 2 = [0][2] 2 mod 3 = 2 0 = [0] [0] 0 mod 3 = 0 1 = [0] [1] 1 mod 3 = 1 2 = [0] [2] 2 mod 3 = 2

3 = [1][0] and so on... 4 = [1][1] 5 = [1][2] 3 = [1] [0]依此类推...... 4 = [1] [1] 5 = [1] [2]

6 = [2][0] 7 = [2][1] 8 = [2][2] 6 = [2] [0] 7 = [2] [1] 8 = [2] [2]

But I'm not sure how to get the horizontal index. 但我不知道如何获得水平索引。

The horizontal index is given by floor(i / v) , or as just i/v if your programming language implements integer division by truncation. 水平索引由floor(i / v) ,或者如果您的编程语言通过截断实现整数除法,则仅为i/v

For example, floor(7/3) = 2, so 7 is on the row 2. 例如,floor(7/3)= 2,因此7在行2上。

This is working solution in java. 这是java中的工作解决方案。 Note that % is mod function. 请注意, %mod函数。

public static void main(String[] args) throws IOException {
    int[] oneD = {1,2,3,4,5,6};
    int w = 3;
    int h = 2;
    int[][] twoD = new int[h][w];
    int[] oneDReversed = new int[oneD.length];

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            twoD[i][j] = oneD[i*w+j];
        }
    }

    for (int i = 0; i < w*h; i++) {
        oneDReversed[i] = twoD[(i / w)][(i%w)];
    }
}

Why the twoD[i][j] = oneD[i*w+j] ? 为什么twoD[i][j] = oneD[i*w+j] Because you have cycle in cycle doing "For every row i selet all collumns j and gives it to the array[num_of_rows][num_of_columns] by the equaliation : row*width + column . 因为你在循环中循环做“对于每一行i所有的列j并通过等于: row*width + column将它赋予array[num_of_rows][num_of_columns]

The reserved means: The row is counted as rounded down index devide number_of_columns . 保留意味着:该row被计为向下舍入的index devide number_of_columns And the column is the rest of deviding the same variables (mod). column是其余的相同变量(mod)。

Here is what I do in C#, I would say its the simplest and most performant way of converting between 1D and 2D arrays it does not need to do any math in the loop and it might matter if you have arrays with millions of items (like reading and writing images) 这是我在C#中所做的,我会说它是在1D和2D数组之间转换的最简单和最高效的方式,它不需要在循环中进行任何数学运算,如果你有数百万项的数组(如阅读和写作图像)

C# Code: C#代码:

// Creates a 2D array from a 1D array
public static int[,] Array1Dto2D(int[] array1D, int width, int height)
{
    int[,] array2D = new int[width, height];
    int i = 0;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            array2D[x, y] = array1D[i];
            i++;
        }
    }
    return array2D;
}

// Creates a 1D array from a 2D array
public static int[] Array2Dto1D(int[,] array2D)
{
    int width = array2D.GetLength(0);
    int height = array2D.GetLength(1);
    int[] array1D = new int[width * height];
    int i = 0;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            array1D[i] = array2D[x, y];
            i++;
        }
    }
    return array1D;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM