I don't know much about math, but from what I read, this seems to be the place to post my question.
I'm working on a computer program where I have a two-dimensional matrix of variable dimensions. What I need to figure out is, based on the dimensions of the matrix, how many potential neighboring connections can be made. So, for example, (0,0) can connect to (0,1) and (1,0) while (3,4) can connect with (2,4) (4,4) (3,3) (3,5).
As it stands, I have the program counting these connections manually, but such a method slows down the program, and it would go quicker if there was a formula that could compute the potential neighbors directly. Is there? Thanks.
What I mean is that a neighbor is only one cell away (either 'up', 'down', 'left', or 'right'). So, with a coordinate (a,b), the neighbors would be (a+1,b),(a-1,b),(a,b+1),(a,b-1), unless such coordinates would be outside the bounds of the matrix. This is why (0,0) only has two neighbors since it can't go 'up' or 'left'. Does that help clear it up?
Surely every entry in the matrix has either 2,3 or 4 neighbours.
The four corner values have 2 neighbours. All the other entries along the edges of the matrix have 3 nieghbours, and all the other entries have 4 neighbours. So lets count up the number of neighouring pairs which counts each connection twice.
So if you have an m*n matrix the number of neighbour pairs is 4*m*n-2*(m+n) so the number of possible connections is 2*m*n-(m+n)
Putting m=n=2 we get 4 which is correct
putting n=1 we get m-1 which is also correct.
Maybe I'm missing something here, but basically if you have an matrix then there are coordinates that are not at the edge and so have 4 connections. There are edge coordinates that are not corners, each of which has 3 connections, and there are 4 coordinates (the four corners) that only have 2 connections. Thus we have , which simplifies to , connections in total.
Note that , as we would expect.
Well, in computer terms, a matrix is a list of lists (or an array of arrays). This would be a 5x5 matrix:
(A) has two potential connections with neighbors, to the right and below. (B) has three potential connections (up, down, and left). Finally, (C) has four potential (up, down, right, and left).Code:AD000 0000B 00000 00C00 00000
However, I only want to count unique connections. So, for example, even though (A) has two neighboring nodes and (D) has three, the connection between them should only be counted once.