[R-sig-Geo] nearest distance in matrix
White.Denis at epamail.epa.gov
White.Denis at epamail.epa.gov
Wed Jul 14 19:29:40 CEST 2004
Here's another version that probably can be simplified:
nmatdist <- function (m)
{
v <- as.vector (m)
ind <- as.matrix (expand.grid (seq (nrow (m)), seq (ncol (m))))
d <- as.matrix (dist (ind, upper=TRUE))
ones <- as.numeric (dimnames(ind[v == 1,])[[1]])
matrix (sapply (seq(nrow(ind)),
function (x) min (d[x, ones])), nrow=nrow(m))
}
Denis White
US EPA, 200 SW 35th St, Corvallis, Oregon, 97333 USA
voice: 541.754.4476, email: white.denis at epa.gov
web: www.epa.gov/wed/pages/staff/white/
r-sig-geo-bounces at stat.math.ethz.ch wrote on 2004-07-14 09:50:16:
> Hello,
>
> I'm trying to use R to simulate biological invasions, and got stuck
with
> the following:
>
> Is there a function that uses a matrix of occupied/empty values (1 or
0)
> as input, and producing an output matrix of the same size, with each
cell
> containing the distance to the nearest occupied cell (1) of the input
> matrix ?
>
> For example, the input matrix:
>
> 0 1 1 0
> 0 0 1 0
> 0 0 0 0
> 0 0 0 0
>
> Would produce an output matrix like:
>
> 1 0 0 1
> 1.4 1 0 1
> 2.2 1.4 1 1.4
> 2.8 2.2 2.0 2.2
>
> For those familiar with AV Spatial Analyst, this function would be the
> equivalent of OutGrid = EucDistance (directionFN, allocationFN,
> maxDistance).
>
> Many thanks in advance for any help or pointers,
>
> Marius
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://www.stat.math.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list