Learn R Programming

imagine (version 1.3.1)

convolution2D: Make convolution calculations from numeric matrix

Description

This function takes a matrix object, and for each cell multiplies its neighborhood by the kernel. Finally, it returns for each cell the mean of the kernel-weighted sum.

Usage

convolution2D(dataMatrix, kernel, times = 1, noNA = FALSE)

convolutionQuantile(dataMatrix, kernel, x, times = 1)

convolutionMedian(dataMatrix, kernel, times = 1)

Arguments

dataMatrix

A numeric matrix object used for apply filters.

kernel

A little matrix used as mask for each cell of dataMatrix.

times

How many times do you want to apply the filter?

noNA

logical indicating whether to make convolution only if all values within kernel are no NA.

x

numeric vector of probabilities with values in [0,1].

Value

convolution2D returns a matrix object with the same dimensions of dataMatrix.

convolutionQuantile uses the kernel but, for each cell, it returns the position of quantile 'x' (value between 0 and 1).

convolutionMedian is a wrapper of convolutionQuantile with x = 0.5.

Details

Convolution is a mathematical operation which allows the multiplication of two arrays of numbers, in order to produce an array of numbers of the same dimensionality. Functions use C++ algorithms. More details are shown in vignette.

Examples

Run this code
# NOT RUN {
# Generate example matrix
nRows <- 50
nCols <- 100

myMatrix <- matrix(runif(nRows*nCols, 0, 100), nrow = nRows, ncol = nCols)
kernel <- diag(3)

# Make convolution
myOutput1 <- convolution2D(myMatrix, kernel)
myOutput2 <- convolutionQuantile(myMatrix, kernel, x = 0.7)

# Plot results
par(mfrow = c(2, 2))
image(myMatrix, zlim = c(0, 100))
image(myOutput1, zlim = c(0, 100))
image(myOutput2, zlim = c(0, 100))
# }

Run the code above in your browser using DataLab