subsums: Sums of submatrices

subsumsR Documentation

Sums of submatrices

Description

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

Usage

subsums(a,p,func="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-th dimension being a[n]. If the length of p is one, recycling is used.

If not a vector, is assumed to be a matrix with d columns, each row representing the coordinates of the elements to be summed. See examples.

func

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.

If sum="", return the array of dimension c(dim(a),prod(p)) where each hyperplane is a shifted version of a.

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 sum of all corner elements of a.

If FALSE, do not wrap a and return an array of dimension dim(a)+p-1.

pad

If wrap is TRUE, pad is the value used to pad the array with. Use a “neutral” value here; for example, if func=sum, then use 0; if max, use -Inf.

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().

Author(s)

Robin K. S. Hankin

Examples

  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,func="max",pad=Inf,wrap=TRUE)  
  subsums(x,2,func="max",pad=Inf,wrap=FALSE)

magic documentation built on Nov. 16, 2022, 9:06 a.m.