Question:
How do I check the bounds on a 2d matrix?
charchar88
2011-07-11 17:01:27 UTC
I'm programming in java but the logic should be the same in any language.

Basically I have a 5x5 matrix:

10100
01101
00101
11001
00110

I need to check all the cells and change it to a 1 or 0 depending on how many of the neighbors are 1's and 0's. However I'm not sure how to get around checking the edge cells since I get index out of bounds errors because it's checking outside of the bounds. How do I prevent this?

If you could provide a psuedo code or even a code example, I promise 10 points. Thanks!
Three answers:
Ratchetr
2011-07-11 17:42:04 UTC
Every cell has between 3 and 8 neighbors. For each potential neighbor, you first need to determine if it is a valid neighbor. If it isn't, you don't add to your count. If it is valid, then you add the cells value to your count of neighbors.



Look at the matrix of neighbors:



0 1 2

3 X 4

5 6 7



For neighbor 0 to be valid, then row can't be 0, and column can't be 0, right? No cell in the first row or the first column has a valid neighbor one row up and one column to the left.



For neighbor 1 to be valid, then row can't be 0, right? No cell in the first row has a neighbor in the row above it.



For neighbor 7 to be valid, then row can't be the last row, or the last column. The last row doesn't have a row after it. The last column doesn't have a column after it, so there is no neighbor at row + 1, col + 1, is there?



And so on. You can derive a simple rule for each neighbor that way.



In pseudo code, you could do something like this, assuming you are trying to count the neighbors of cell r,c, and have variables maxRow = Rows-1 (max valid INDEX) and maxCol = Cols-1 (max valid INDEX). For a 5X5,m maxRow and maxCol would be 4.



count = 0



// neighbor 0:

count += ( r > 0 && c > 0 ) ? cell[r-1][c-1] : 0;

// neighbor 1:

count += (r > 0) ? cell[r-1][c] : 0;

// neighbor 2:

count += (r > 0 && c < maxCol) ? cell[r-1][c+1] : 0;

... And so on down to:

// neighbor 7

count += (r < maxRow && c < maxCol) ? cell[r+1][c+1] : 0;



So basically you would try to add to count 8 times, once for each neighbor. The ? : ternary operator lets you first test the condition: Is r and c valid for this neighbor. If it is, add the value of that neighgbor. If it isn't add 0 (which, obviously, is just a NOP).



You could do the same thing using if/else statements, but I always find 8 different branches through if/else logic hard to read and hard to debug. The approach above has the advantage that each line of code is independent. It's either right, or it's wrong. You can prove to yourself that it's right or wrong by looking at just 1 line of code. No need to follow if/else logic to 3 levels.



Do note: It fails for a 2x2 matrix. They are a special case. (as is 1x1).



Hope that makes sense. (And that I'm not off by 1 in there somewhere ;-)
modulo_function
2011-07-12 01:23:08 UTC
You can use modulo, %, to get 'wrap around' behavior. This means that to the right of the rightmost element is first element in the row



Let's say that you want to check the neighbors of element r,c for a square matrix, of dimension row x row



The neighbors of r, c are;

left, up, right, down

ar[r][(c+row+1)%row], ar[(r+1)%row][c], ar[r][(c+1)%row], ar[(r+1)%row][c]



note that instead of subtracting to go to a lower numbered row or column you add row+1. You can easily see that ( indexing from 0 )



lets say r=2, c=4

then

left [r][(c+row+1)%5] = [2][0] and so you get the first element of the row



Sometimes this 'wrap around' behavior is what you want.



+add

Then again, maybe that's not what you want! If it's not then you need to perform 3 types of checks:



1) the 4 corners

2) the edges, but not corner

3) interior points



I've had to do this with engineering calculations of 2 dimensional heat transfer problems where the edges represent one of the 3 types of boundary conditions: fixed T, adiabatic, convective.
Phil Elm
2011-07-12 00:05:02 UTC
Well.. If it's always 5x5, just cap it in the for loop.



for(int a = 0; a < 5; a++){

for(int b = 0; b < 5; b++){

//Do your work here.. For example, to set all matrix positions to 1, do this.

matrix[a][b] = 1;

}



}



This should be what you're looking for, unless I read the question incorrectly.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...