# [R] regularly lattice & neighbors

David James dj at research.bell-labs.com
Thu Mar 30 16:18:45 CEST 2000

```Hi,

The first thing that comes to my mind is the
trick of using complex numbers:

> z <- complex(real=rep(1:3, 3), imaginary = rep(1:3, c(3,3,3)))
> z1 <- z[2]   # say,
> "rook"
> z[abs(z1-z)<=1]
[1] 1+1i 2+1i 3+1i 2+2i

> "queen"
> z[abs(z1-z)<=sqrt(2)
[1] 1+1i 2+1i 3+1i 1+2i 2+2i 3+2i

By the way, if you want indices, just use match()

> match(z[abs(z1-z)<=1], z)
[1] 1 2 3 5

(I should have deleted the "chosen" point z[2] from the
example above, but you get my point:-)

David A James                        Phone: (908) 582-3082
Bell Labs, Lucent Technologies       Fax:   (908) 582-3340
600 Mountain Ave                     Email: dj at bell-labs.com
Murray Hill, NJ 07974
-------------------------------------------------------------
> From: "Alvaro A. Novo" <novo at uiuc.edu>
> To: r-help at stat.math.ethz.ch
> Subject: [R] regularly lattice & neighbors
> Date: Wed, 29 Mar 2000 17:12:06 -0600
> MIME-Version: 1.0
> Content-Transfer-Encoding: 8bit
>
> Dear R-users,
>
> Is there a procedure to identify neighbors in a regular lattice using either a
> "rook" or a "queen" criterium? To be more specific, suppose:
>
> My lattice:
>
> 1	2	3
> 4	5	6
> 7	8	9
>
> "Rook" Neighbors
>
> 1 has neighbors 2,4
> 2 has neighbors 1,3,5
> ...
> 5 has neighbors 2,4,6,8
>
> "Queen Neighbors"
>
> 1 has neighbors 2,4,5
> 2 has neighbors 1,3,4,5,6
>
> For each case, the final output would be a 9x9 matrix with 0's and 1's.
>
> I have access to a software that does this for me, but it would be nice to
> avoid importing such matrices.
>
> Thank you for your help,
>
> Alvaro Novo
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```