简体   繁体   中英

Map 2D matrix to 1D array

I have two containers: a 2-dimensional NxN matrix and a 1-dimensional array which is the transposed 1D "version" of the matrix (so for a 5x5 array, I will have a 25 element array with the same values). I want to implement a query function that will take 2D coordinates as arguments but will be doing work on the equivalent 1D array.

In order to keep algorithm efficiency strictly non-quadratic I want to access only the array and not the matrix.

I've checked other questions but they all talk about converting the whole matrix to an array through nested for-loops. I don't want to do this, as that would take quadratic time to run. Instead, I want the conversion to be on-demand for a given coordinate through a query function/method. In other words for a given number of N columns/rows:

transpose(int i, int j) {
    int result;

    result = i * N + j;
    return result;
}

This is the formula I'm using but it is not correct. For example if I want to access the element in the {5,5} position the result would be 5*5 + 5 = 30, which is greater than 25 (which would be the total number of elements for 5x5 matrix).

Thanks in advance.

If you have a 2d array and a 1d array having same elements,then the following will be true

  2d[i][j]=1d[i*number_of_columns+j]

I am assuming from your post that you already have created a 1d array out of a 2d one. Note i and j are indices and rememeber indices begin from 0

EDIT:If you are accessing an element at [5][5] (as last element)it means your array is of order 6 by 6 and not 5 by 5 .So your 1d array will have 6*6=36 elements and not 25 .

You can use the deepToString() method to output a 2D array to a String. This can make it easier to do things such as sort() for example.

Assuming a declared int mat2d[m][n]; with m rows and n columns, you can convert it like

int * mat1d = new int[m * n];
int k = 0;
for (int i = 0; i < m; ++i)
    for (int j = 0; j < n; ++i)
        mat1d[k++] = mat2d[i][j];

If you just want to convert between 1D and 2D coordinates, serve yourself and make functions from this:

const int width = 10;

// from 1D coordinate to 2D coordinate
int coord_1d = 25;
int coord_x = coord_1d % width;
int coord_y = coord_1d / width;

// from 2D coordinate to 1D coordinate
coord_1d = coord_x + coord_y * width;

Your question is quite confusing, you said that you don't want nested loops, here is a just-one-loop conversion

    int[][] a={
            {1,2,3,4,5,6},
            {4,5,6,7,8,9},
            {7,8,9,1,2,3},
            {1,2,3,4,5,6}
            };
    int[]b=new int[a.length*a[0].length];

    int x=0;
    for(int i=0, j=0;i<a.length&&j<a[0].length;i=(j==a[0].length-1?i+1:i),j=(j+1)%a[0].length)
        b[x++]=a[i][j];

    System.out.println(Arrays.toString(b));

If you want the conversion to be based on coordinates, by changing i and j values in the for loop to such coordinates will allow you to convert to array only a subset of your matrix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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