Skip to content
Browse files

moore updated with extra question

  • Loading branch information...
1 parent 67033dd commit ef1fa05baac8e29774d4191157fadb1010bd6840 @cboettig committed
Showing with 88 additions and 28 deletions.
  1. +4 −3 mendeley.rb
  2. +31 −3 moore.Rmd
  3. +53 −22 moore.md
View
7 mendeley.rb
@@ -537,6 +537,7 @@ def render(context)
category = m.folder_documents(@category, 1, 10000) # max of 10000 items, should get max from all_categories[]['size']
# category = m.folder_documents("56626111",1, 10)
+ file = @category + "_library.json"
if !File.exist?("library.json")
doc_ids = JSON.parse(category.body)["document_ids"]
@@ -548,12 +549,12 @@ def render(context)
end
## Sort everything in the library by date added
library = library.sort_by { |k| k["date_added"] }
- # Write out
- File.open("library.json","w") do |f|
+ # Write out
+ File.open(file, "w") do |f|
f.write(JSON.pretty_generate(library))
end
else
- buffer = open('library.json')
+ buffer = open(file)
library = JSON.load(buffer)
end
View
34 moore.Rmd
@@ -18,7 +18,7 @@ j <- sample(1:dim(M)[2], 1) # another random number between 1 and 8
And then grab all the points in the Moore neighborhood
-```
+```{r}
r <- 1
neighborhood <-
cbind(c(i-r, i, i+r, i-r, i, i+r, i-r, i, i+r),
@@ -40,9 +40,9 @@ wrapped_neighborhood[,2] <- (neighborhood[,2] - 1) %% dim(M)[2] + 1
```
-Clearly this can be extended to $r > 1$.
+This can be extended to $r > 1$.
-```
+```{r}
r_max <- 3
rows <- numeric(0)
cols <- numeric(0)
@@ -61,7 +61,35 @@ 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
+```
+
+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])
+}
+
+neighborhood <- cbind(rows, cols)
+```
+
+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
75 moore.md
@@ -21,13 +21,14 @@ j <- sample(1:dim(M)[2], 1) # another random number between 1 and 8
And then grab all the points in the Moore neighborhood
-```
+
+```r
r <- 1
-neighborhood <-
-cbind(c(i-r, i, i+r, i-r, i, i+r, i-r, i, i+r),
- c(j-r, j-r, j-r, j, j, j, j+r, j+r, j+r))
+neighborhood <- cbind(c(i - r, i, i + r, i - r, i, i + r, i - r, i, i + r),
+ c(j - r, j - r, j - r, j, j, j, j + r, j + r, j + r))
```
+
We can then access the cells from M in our neighborhood. We tell R to display these points as a matrix
@@ -36,7 +37,10 @@ matrix(M[neighborhood], nrow = 3)
```
```
-## Error: object 'neighborhood' not found
+## [,1] [,2] [,3]
+## [1,] -1.5540 0.69641 0.37012
+## [2,] -0.7645 -0.23802 -1.65624
+## [3,] -0.1179 0.03133 0.03626
```
@@ -45,48 +49,75 @@ matrix(M[neighborhood], nrow = 3)
```r
wrapped_neighborhood <- neighborhood
+wrapped_neighborhood[, 1] <- (neighborhood[, 1] - 1)%%dim(M)[1] + 1
+wrapped_neighborhood[, 2] <- (neighborhood[, 2] - 1)%%dim(M)[2] + 1
```
-```
-## Error: object 'neighborhood' not found
-```
+
+
+This can be extended to $r > 1$.
+
```r
-wrapped_neighborhood[, 1] <- (neighborhood[, 1] - 1)%%dim(M)[1] + 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)
```
-## Error: object 'neighborhood' not found
-```
+
+
+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
```
-```
-## Error: object 'neighborhood' not found
-```
-Clearly this can be extended to $r > 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)
-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))
-}
+wrap <- function(i, r, n) {
+ if (i - r < 0)
+ out <- i + r + 1
+ if (i + r > n)
+ out <- i - r - 1
+ out
+}
-neighborhood <- cbind(rows, cols)
+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])
+}
```
+```
+## Error: unused argument (dim(M)[1])
+```
+```r
+neighborhood <- cbind(rows, cols)
+```
+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 ef1fa05

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