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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This seems to work -- there's probably a faster way to do it ... N <- 3 x <- matrix(1:(N^2),nrow=N,ncol=N) rowdiff <- function(y,z,mat)abs(row(mat)[y]-row(mat)[z]) coldiff <- function(y,z,mat)abs(col(mat)[y]-col(mat)[z]) rook.case <- function(y,z,mat){coldiff(y,z,mat)+rowdiff(y,z,mat)==1} bishop.case <- function(y,z,mat){coldiff(y,z,mat)==1 & rowdiff(y,z,mat)==1} queen.case <- function(y,z,mat){rook.case(y,z,mat) | bishop.case(y,z,mat)} matrix(as.numeric(sapply(x,function(y)sapply(x,rook.case,y,mat=x))),ncol=N^2,nrow=N^2) matrix(as.numeric(sapply(x,function(y)sapply(x,bishop.case,y,mat=x))),ncol=N^2,nrow=N^2) matrix(as.numeric(sapply(x,function(y)sapply(x,queen.case,y,mat=x))),ncol=N^2,nrow=N^2) On Wed, 29 Mar 2000, Alvaro A. Novo wrote:> 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > >-- Ben Bolker bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker 318 Carr Hall/Box 118525 tel: (352) 392-5697 Gainesville, FL 32611-8525 fax: (352) 392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._