Skip to content
Browse files

updated for wrapping local neighborhood (corrected)

  • Loading branch information...
1 parent ef1fa05 commit fe98bfde9d3872791a1a3c90f3ca06021f948086 @cboettig committed
Showing with 125 additions and 44 deletions.
  1. +16 −18 moore.Rmd
  2. +109 −26 moore.md
View
34 moore.Rmd
@@ -53,6 +53,7 @@ for(r in 1:r_max){
}
neighborhood <- cbind(rows, cols)
+neighborhood
```
Once again we have to deal with boundary cases, e.g. wrapping as before:
@@ -61,6 +62,7 @@ Once again we have to deal with boundary cases, e.g. wrapping as before:
wrapped_neighborhood <- neighborhood
wrapped_neighborhood[,1] <- (neighborhood[,1] - 1) %% dim(M)[1] + 1
wrapped_neighborhood[,2] <- (neighborhood[,2] - 1) %% dim(M)[2] + 1
+wrapped_neighborhood
```
@@ -68,28 +70,24 @@ wrapped_neighborhood[,2] <- (neighborhood[,2] - 1) %% dim(M)[2] + 1
What if we wanted to wrap just the neighborhood if we hit the boundary?
```{r}
-r_max <- 3
-rows <- numeric(0)
-cols <- numeric(0)
-
-wrap <- function(i, r, n){
- if(i-r < 0)
- out <- i+r+1
- if(i+r > n)
- out <- i-r-1
- out
+wrap <- function(k, r, n){
+ if(k < 1)
+ k <- k + r - (k-1)
+ if(k > n)
+ k <- k - r - (k-n)
+ k
}
+```
-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))
- rows <- wrap(rows, i, r, dim(M)[1])
- cols <- wrap(cols, j, r, dim(M)[2])
-}
+Apply this function to the rows/columns
-neighborhood <- cbind(rows, cols)
+```{r}
+wrapped_neighborhood <- neighborhood
+wrapped_neighborhood[,1] <- sapply(neighborhood[,1], wrap, r, dim(M)[1])
+wrapped_neighborhood[,2] <- sapply(neighborhood[,2], wrap, r, dim(M)[2])
+wrapped_neighborhood
```
-Note that this does not handle the case of a neighborhood that is larger than the matrix in some dimension; e.g. we don't guarentee $i+r+1<n$, etc.
+Note that this does not handle the case of a neighborhood that is larger than the matrix in some dimension; e.g. we don't guarentee `i+r+1<n`, etc.
View
135 moore.md
@@ -37,10 +37,7 @@ matrix(M[neighborhood], nrow = 3)
```
```
-## [,1] [,2] [,3]
-## [1,] -1.5540 0.69641 0.37012
-## [2,] -0.7645 -0.23802 -1.65624
-## [3,] -0.1179 0.03133 0.03626
+## Error: subscript out of bounds
```
@@ -69,6 +66,38 @@ for (r in 1:r_max) {
}
neighborhood <- cbind(rows, cols)
+neighborhood
+```
+
+```
+## rows cols
+## [1,] 7 3
+## [2,] 8 3
+## [3,] 9 3
+## [4,] 7 4
+## [5,] 8 4
+## [6,] 9 4
+## [7,] 7 5
+## [8,] 8 5
+## [9,] 9 5
+## [10,] 6 2
+## [11,] 8 2
+## [12,] 10 2
+## [13,] 6 4
+## [14,] 8 4
+## [15,] 10 4
+## [16,] 6 6
+## [17,] 8 6
+## [18,] 10 6
+## [19,] 5 1
+## [20,] 8 1
+## [21,] 11 1
+## [22,] 5 4
+## [23,] 8 4
+## [24,] 11 4
+## [25,] 5 7
+## [26,] 8 7
+## [27,] 11 7
```
@@ -79,6 +108,38 @@ Once again we have to deal with boundary cases, e.g. wrapping as before:
wrapped_neighborhood <- neighborhood
wrapped_neighborhood[, 1] <- (neighborhood[, 1] - 1)%%dim(M)[1] + 1
wrapped_neighborhood[, 2] <- (neighborhood[, 2] - 1)%%dim(M)[2] + 1
+wrapped_neighborhood
+```
+
+```
+## rows cols
+## [1,] 7 3
+## [2,] 8 3
+## [3,] 1 3
+## [4,] 7 4
+## [5,] 8 4
+## [6,] 1 4
+## [7,] 7 5
+## [8,] 8 5
+## [9,] 1 5
+## [10,] 6 2
+## [11,] 8 2
+## [12,] 2 2
+## [13,] 6 4
+## [14,] 8 4
+## [15,] 2 4
+## [16,] 6 6
+## [17,] 8 6
+## [18,] 2 6
+## [19,] 5 1
+## [20,] 8 1
+## [21,] 3 1
+## [22,] 5 4
+## [23,] 8 4
+## [24,] 3 4
+## [25,] 5 7
+## [26,] 8 7
+## [27,] 3 7
```
@@ -88,36 +149,58 @@ What if we wanted to wrap just the neighborhood if we hit the boundary?
```r
-r_max <- 3
-rows <- numeric(0)
-cols <- numeric(0)
-
-wrap <- function(i, r, n) {
- if (i - r < 0)
- out <- i + r + 1
- if (i + r > n)
- out <- i - r - 1
- out
-}
-
-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))
- rows <- wrap(rows, i, r, dim(M)[1])
- cols <- wrap(cols, j, r, dim(M)[2])
+wrap <- function(k, r, n) {
+ if (k < 1)
+ k <- k + r - (k - 1)
+ if (k > n)
+ k <- k - r - (k - n)
+ k
}
```
-```
-## Error: unused argument (dim(M)[1])
-```
+
+Apply this function to the rows/columns
+
```r
+wrapped_neighborhood <- neighborhood
+wrapped_neighborhood[, 1] <- sapply(neighborhood[, 1], wrap, r, dim(M)[1])
+wrapped_neighborhood[, 2] <- sapply(neighborhood[, 2], wrap, r, dim(M)[2])
+wrapped_neighborhood
+```
-neighborhood <- cbind(rows, cols)
+```
+## rows cols
+## [1,] 7 3
+## [2,] 8 3
+## [3,] 5 3
+## [4,] 7 4
+## [5,] 8 4
+## [6,] 5 4
+## [7,] 7 5
+## [8,] 8 5
+## [9,] 5 5
+## [10,] 6 2
+## [11,] 8 2
+## [12,] 5 2
+## [13,] 6 4
+## [14,] 8 4
+## [15,] 5 4
+## [16,] 6 6
+## [17,] 8 6
+## [18,] 5 6
+## [19,] 5 1
+## [20,] 8 1
+## [21,] 5 1
+## [22,] 5 4
+## [23,] 8 4
+## [24,] 5 4
+## [25,] 5 7
+## [26,] 8 7
+## [27,] 5 7
```
-Note that this does not handle the case of a neighborhood that is larger than the matrix in some dimension; e.g. we don't guarentee $i+r+1<n$, etc.
+Note that this does not handle the case of a neighborhood that is larger than the matrix in some dimension; e.g. we don't guarentee `i+r+1<n`, etc.

0 comments on commit fe98bfd

Please sign in to comment.
Something went wrong with that request. Please try again.