我正在研究一种路径规划算法,它将“目标路径”作为一系列数字返回。我需要找到这些数字对应的矩阵的哪个元素(行和列)。所以我正在寻找描述这种关系的数学表达式。
关系如下:
1 --> row = 1 and column = 1
2 --> row = 1 and column = 2
3 --> row = 1 and column = 3
4 --> row = 1 and column = 4
.
.
.
37901 --> row = 1 and column = 37901
37902 --> row = 2 and column = 1
37903 --> row = 2 and column = 2
37904 --> row = 2 and column = 3
.
.
.
1436485801 --> row = 37901 and column = 37901
请注意,1436485801 = 37901 ^ 2,其中37901 = 151 * 251。谢谢!
要将(row, column)
对映射到1D索引,您可以使用:
index = column-size * (row - 1) + column
所以,在你的情况下,它是
index = 37901 * (row - 1) + column
Matlab有适合你的function sub2ind
。
// for a matrix A
index = sub2ind(size(A), row, column)
要执行相反的操作,将1D索引转换为行和列,您可以使用function ind2sub
(您必须知道矩阵的大小):
// for a matrix A
[row, column] = ind2sub(size(A), index)
或者,如果您想手动执行此操作,则值为:
row = fix((index-1)/size(A,1)) + 1; // quotient
column = rem( index-1, size(A,1)) + 1; // remainder