[R-sig-Geo] Moving-window sum in a matrix

Marius Gilbert mgilbert at ulb.ac.be
Thu Dec 16 16:05:55 CET 2004


Dear colleagues,

I have a 500 by 500 matrix with presence or absence value in each cell as 
an input matrix. I am trying to create an output matrix having in each 
cell the sum of the values from neigbouring cells in the input matrix 
according to a moving-window defined as:

0 1 1 1 0
1 1 1 1 1
1 1 0 1 1
1 1 1 1 1
0 1 1 1 0

where the output cell receiving the values is located in the center. I 
have written a function that loop over the cells of the output matrix, and 
sum up the values from these neigbouring cells. It works, but it is fairly 
slow, and hangs if I try it over a larger matrix. Given that this is a 
rather basic function available in most raster GIS packages (IDRISI and AV 
Spatial Analyst) and running fairly quickly, I wondered whether anyone had 
previously come accross a similar function in an existing R-package.

Any helps  or pointer would be appreciated,

Many thanks in advance,

Marius




More information about the R-sig-Geo mailing list