Skip to content

Commit

Permalink
moore updated with extra question
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Jul 17, 2013
1 parent 67033dd commit ef1fa05
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 28 deletions.
7 changes: 4 additions & 3 deletions mendeley.rb
Expand Up @@ -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"]
Expand All @@ -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

Expand Down
34 changes: 31 additions & 3 deletions moore.Rmd
Expand Up @@ -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),
Expand All @@ -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)
Expand All @@ -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.

75 changes: 53 additions & 22 deletions moore.md
Expand Up @@ -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


Expand All @@ -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
```


Expand All @@ -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.