my first question on Stack Overflow.
I'm trying to update a 25x25 matrix representing states in a 5x5 grid. The rows represent current states, columns represent the next state. I'm using a formula given below to assess the adjacency of a given state to another given state by their coordinates. The aim is to then use those coordinates to update the 25x25 state matrix with 1's where moving from one numbered state in the 5x5 grid to it's adjacent state is possible.
|x1−x2|≤1 and |y1−y2|≤1
No errors, and only the T[25,25] element is updating. Any ideas why this isn't working properly?
T = matrix(c(1:25),nrow=5,ncol=5, byrow=T)
S = matrix(0,nrow=25,ncol=25)
for (i in nrow(T)){
for (j in ncol(T)){
for (f in nrow(T)){
for (g in ncol(T)){
if ((abs(i-f) <= 1) & (abs(j-g) <= 1)){
S[T[i,j], T[f,g]] = 1
}}}}}