[英]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).我有两个容器:一个二维 NxN 矩阵和一个一维数组,它是矩阵的转置一维“版本”(因此对于 5x5 数组,我将有一个具有相同值的 25 个元素数组)。 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.我检查了其他问题,但他们都在谈论通过嵌套的 for 循环将整个矩阵转换为数组。 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:
换句话说,对于给定数量的 N 列/行:
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).
例如,如果我想访问 {5,5} 位置的元素,结果将是 5*5 + 5 = 30,它大于 25(这将是 5x5 矩阵的元素总数)。
Thanks in advance.提前致谢。
If you have a 2d
array and a 1d
array having same elements,then the following will be true如果你有一个
2d
数组和一个具有相同元素的一1d
数组,那么以下将成立
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
注意
i
和j
是索引,记住索引从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
.编辑:如果您正在访问
[5][5]
元素(作为最后一个元素),这意味着您的数组的顺序是6 by 6
而不是5 by 5
。所以您的 1d 数组将有6*6=36
元素而不是25
.
You can use the deepToString()
method to output a 2D array to a String.您可以使用
deepToString()
方法将二维数组输出到字符串。 This can make it easier to do things such as sort()
for example.例如,这可以更轻松地执行诸如
sort()
的操作。
Assuming a declared int mat2d[m][n];
假设声明了
int mat2d[m][n];
with m
rows and n
columns, you can convert it like有
m
行和n
列,你可以像
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:如果您只想在 1D 和 2D 坐标之间进行转换,请为自己服务并从中创建函数:
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如果您希望转换基于坐标,通过将 for 循环中的
i
和j
值更改为这样的坐标,您可以将矩阵的子集转换为数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.