[R] regularly lattice & neighbors
Ben Bolker
ben at zoo.ufl.edu
Thu Mar 30 05:05:16 CEST 2000
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list