Skip to content

Commit

Permalink
updated for general radius
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Jul 17, 2013
1 parent 859af52 commit 67033dd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
22 changes: 21 additions & 1 deletion moore.Rmd
Expand Up @@ -42,6 +42,26 @@ wrapped_neighborhood[,2] <- (neighborhood[,2] - 1) %% dim(M)[2] + 1

Clearly this can be extended to $r > 1$.

```
r_max <- 3
rows <- numeric(0)
cols <- numeric(0)
for(r in 1:r_max){
rows <- c(rows, c(i-r, i, i+r, i-r, i, i+r, i-r, i, i+r))
cols <- c(cols, c(j-r, j-r, j-r, j, j, j, j+r, j+r, j+r))
}
neighborhood <- cbind(rows, cols)
```

Once again we have to deal with boundary cases, e.g. wrapping as before:

```{r}
wrapped_neighborhood <- neighborhood
wrapped_neighborhood[,1] <- (neighborhood[,1] - 1) %% dim(M)[1] + 1
wrapped_neighborhood[,2] <- (neighborhood[,2] - 1) %% dim(M)[2] + 1
neighborhood
38 changes: 36 additions & 2 deletions moore.md
Expand Up @@ -36,7 +36,7 @@ matrix(M[neighborhood], nrow = 3)
```

```
## Error: subscript out of bounds
## Error: object 'neighborhood' not found
```


Expand All @@ -45,14 +45,48 @@ matrix(M[neighborhood], nrow = 3)

```r
wrapped_neighborhood <- neighborhood
```

```
## Error: object 'neighborhood' not found
```

```r
wrapped_neighborhood[, 1] <- (neighborhood[, 1] - 1)%%dim(M)[1] + 1
```

```
## Error: object 'neighborhood' not found
```

```r
wrapped_neighborhood[, 2] <- (neighborhood[, 2] - 1)%%dim(M)[2] + 1
```

```
## Error: object 'neighborhood' not found
```



Clearly this can be extended to $r > 1$.

```
r_max <- 3
rows <- numeric(0)
cols <- numeric(0)
for(r in 1:r_max){
rows <- c(rows, c(i-r, i, i+r, i-r, i, i+r, i-r, i, i+r))
cols <- c(cols, c(j-r, j-r, j-r, j, j, j, j+r, j+r, j+r))
}
neighborhood <- cbind(rows, cols)
```






neighborhood

0 comments on commit 67033dd

Please sign in to comment.