## Generate a 10 x 10 grid of nodes separated by 100m.
nodes <- expand.grid(
x = seq(from = 0, to = 900, by = 100),
y = seq(from = 0, to = 900, by = 100)
)
plot(nodes, main = "Node Grid", asp = 1)
## Calculate distances with a 300m cutoff.
## Only neighbors within 300m will have non-zero entries.
d <- distance_matrix(
x = nodes$x,
y = nodes$y,
cutoff = 300
)
## Inspect the sparse matrix structure.
## Note: The matrix is symmetric and the diagonal is zero.
d[1:10, 1:10]
## Count the number of neighbors for the first node.
sum(d[1, ] > 0)
Run the code above in your browser using DataLab