【发布时间】:2017-07-10 19:59:19
【问题描述】:
我有一个大小为m x n 的矩阵M,它保存为一个长度为m * n 的一维数组N。该数组的每个单元格都包含一些整数变量,它们是数据点的 ID。每个单元格中整数变量的数量随时间而变化。
N[0] = {1,4,5,7}
N[1] = {2,9,3,1,7,4}
N[2] = {7,1,3,9,8}
N[3] = {6,4,2}
...
我使用返回的索引函数访问这些元素
idx = x + y * n
给定一些索引idx,我想使用索引为idx 的相邻单元和中心单元的所有整数变量来访问大小为s 的数据点数组D。大小s 可以很大。
为了明确我的观点:而不是在所有数据点上进行这样的循环
for(int i=0; i<s; i++)
// Do something with D[i]
我想要类似(但更紧凑)的东西
// Access central cell
idx = x + y*n;
num_Elements = Number_of_Elements_Cell(x,y);
for(int i=0; i<num_Elements; i++)
// Do something with D[N[idx][i]]
// Access right cell
idx = (x+1) + y*n;
num_Elements = Number_of_Elements_Cell(x+1,y);
for(int i=0; i<num_Elements; i++)
// Do something with D[N[idx][i]]
// Access left cell
idx = (x-1) + y*n;
num_Elements = Number_of_Elements_Cell(x-1,y);
for(int i=0; i<num_Elements; i++)
// Do something with D[N[idx][i]]
等等。对于所有细胞,我必须这样做 9 次。
我的问题:考虑到N 的结构,有没有更好的方法来做到这一点?
【问题讨论】:
-
有什么问题?实现
Number_of_Elements_Cell?
标签: c nearest-neighbor neighbours