Skip to contents

Given a matrix and the size of a block, returns a new matrix containing the average of each block.

Usage

blockmean(mat, block)

Arguments

mat

numeric matrix

block

integer vector of length 2, containing the size of the block (rows, columns). If only one integer is passed, the block is square

Value

A new reduced matrix with mean of each block

Examples

mat <- matrix(1:64, nrow = 8, byrow = TRUE)
mat
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#> [1,]    1    2    3    4    5    6    7    8
#> [2,]    9   10   11   12   13   14   15   16
#> [3,]   17   18   19   20   21   22   23   24
#> [4,]   25   26   27   28   29   30   31   32
#> [5,]   33   34   35   36   37   38   39   40
#> [6,]   41   42   43   44   45   46   47   48
#> [7,]   49   50   51   52   53   54   55   56
#> [8,]   57   58   59   60   61   62   63   64

blockmean(mat, 2) # or blockmean(mat, c(2, 2))
#>      [,1] [,2] [,3] [,4]
#> [1,]  5.5  7.5  9.5 11.5
#> [2,] 21.5 23.5 25.5 27.5
#> [3,] 37.5 39.5 41.5 43.5
#> [4,] 53.5 55.5 57.5 59.5