Learn R Programming

magic (version 1.3-20)

subsums: Sums of submatrices

Description

Returns the sums of submatrices of an array; multidimensional moving window averaging

Usage

subsums(a,p,FUN="sum",wrap=TRUE, pad=0)

Arguments

a
Array to be analysed
p
Argument specifying the subarrays to be summed. If a vector of length greater than one, it is assumed to be of length d=length(dim(a)), and is interpreted to be the dimensions of the subarrays, with the size of the window's $n{^{\rm
FUN
Function to be applied over the elements of the moving window. Default value of sum gives the sum as used in is.2x2.correct(); other choices might be mean, prod, or max.

wrap
Boolean, with default value of TRUE meaning to view array a as a n-dimensional torus. Thus, if x=subsums(a,p,wrap=TRUE), and if dim(a)=c(a_1,...,a_d) then x[a_1,...,a_d] is the
pad
If wrap is TRUE, pad is the value used to pad the array with. Use a neutral value here; for example, if FUN=sum, then use 0; if max, use $-\infty$.

Details

The offset is specified so that allsums(a,v)[1,1,...,1]= sum(a[1:v[1],1:v[2],...,1:v[n]]), where n=length(dim(a)).

Function subsums() is used in is.2x2.correct() and is.diagonally.correct().

Examples

Run this code
data(Ollerenshaw)
  subsums(Ollerenshaw,c(2,2))
  subsums(Ollerenshaw[,1:10],c(2,2))
  subsums(Ollerenshaw, matrix(c(0,6),2,2)) # effectively, is.bree.correct()

  # multidimensional example.  
  a <- array(1,c(3,4,2))
  subsums(a,2)             # note that p=2 is equivalent to p=c(2,2,2);
                           # all elements should be identical

  subsums(a,2,wrap=FALSE)  #note "middle" elements > "outer" elements


  #Example of nondefault function:
  x <- matrix(1:42,6,7)
  subsums(x,2,FUN="max",pad=Inf,wrap=TRUE)  
  subsums(x,2,FUN="max",pad=Inf,wrap=FALSE)

Run the code above in your browser using DataLab