1

The code inside the for loop is for the x and y (j and i) "coordinates" from a 2d array. How could I implement this neighbor/index finding in a 1d array? I think I could implement it for the first four equations. But i'm confused as how to implement up-left etc.

for(int i=0; i<cols*rows; i++){
        //Counts current index's 8 neigbour int values
      int count=0;
      int x = i%cols;
      int y = i/rows;
      //rows y i
      //cols x j

      count+= [grid][i][(j-1+cols)%cols] //left
         +[grid][i][(j+1+cols)%cols]    //right
         +[grid][(i-1+rows)%rows][j]    //up
         +[grid][(i+1+rows)%rows][j]    //down
         +[grid][(i-1+rows)%rows][ (j-1+cols)%cols]  //up-left
         +[grid][(i+1+rows)%rows][ (j+1+cols)%cols]  //down-right
         +[grid][(i+1+rows)%rows][ (j-1+cols)%cols]  //down-left
         +[grid][(i-1+rows)%rows][ (j+1+cols)%cols] ;//up-right
}
2
  • Do you mean you have a 1D array, example int grid[rows * cols]? You have calculated x/y where data should be grid[i] Commented Mar 6, 2018 at 6:43
  • Yes, the size of my 1d array is rows*cols. I have a 1d array, but the neighbor finding is for a 2d array I want to know how to do this equation(s) for a 1d array. Commented Mar 6, 2018 at 6:55

3 Answers 3

2

Starting with a 1-D vector:

int rows = 10;
int cols = 10;
vector<int> grid(rows * cols);

You can manage this in different ways, example

for(int y = 0; y < rows; y++)
{
    for(int x = 0; x < cols; x++)
    {
        int point = grid[y * rows + x];
    }
}

Where you can access any point at any given x and y in a 2-dimensional plane.

Top-left is:

x = 0;
y = 0;

bottom-right is

x = cols - 1;
y = rows - 1;

And so on.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you get the coordinates (x,y) from the index of the loop without making more for loops?
If you know the index i, then x = i%cols and y = i/rows, you already did that yourself! Conversely, if you know x and y, then index is i = y * rows + x as shown above.
That's not quite right to get from index to x, y. It should be x = i%cols and y = i/cols.
1

Use a function like this

inline int idx(const int i, const int j, const int rows) const
{
    return i * rows + j;
}

to convert the 2d indices to 1d indices. This way you don't have to change your algorithm.

Usage would be grid[idx(i, (j-1+cols)%cols, rows)].

Comments

0

The basic formula for computing the 1d coordinate from the 2d index pattern is usually one of the following:

  • row_index * row_length + column_index
  • column_index * column_length + row_index

Which one applies to your case depends on whether you would like to have a row-based or column-based memory layout for your 2d array. It makes sense to factor out the computation of this index into a separate function, as suggested in the other answer.

Then you just need to fill in the values somehow.

You could do it like this, for example:

// iterate big picture
// TODO: make sure to handle the edge cases appropriately
for (int i_row = 1; i_row < n_rows - 1; i_row++) {
    for (int i_col = 1; i_col < n_cols -1; i_col++) {
        // compute values
        dst[i_row*n_cols+i_col] = 0;
        for (int r = i_row-1; r < i_row+2; r++) {
            for (int c = i_col-1; c < i_col+2; c++) {
                dst[i_row*n_cols+i_col] += src[r*n_cols + c];
            }
        }
    }
}

Assuming src and dst are distinct 1d vectors of size n_rows*n_cols...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.