I have a matrix M
of size m x n
which is saved as a one dimensional array N
of length m * n
. Every cell of this array contains some integer variables which are the ID of data points. The amount of integer variables in every cell changes over time.
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}
...
I access these elements by using an index function which returns
idx = x + y * n
Given some index idx
I want to use all integer variables of the neighbor cells and the central cell with index idx
to access an array of data points D
of size s
. Size s
can be very large.
To make my point clear: Instead of such a loop over all data points
for(int i=0; i<s; i++)
// Do something with D[i]
I want something like (but more compact)
// 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]]
and so on. For all cells I have to do that 9 times.
My question: Is there a better way to do that given the structure N
?