mat.mask: Transform matrix data upon masking

View source: R/mat.mask.R

mat.maskR Documentation

Transform matrix data upon masking

Description

Transforms a matrix called org, represented by \mathbf{Z} = (z_{ij})_{p \times q}, using a matrix called mask, represented by \mathbf{M} = (m_{ij})_{p \times q}.
Both matrices, org and mask have the same dimensionality, i.e. p \times q.
However both matrices belong to different vector spaces,
Since org is real or complex, but mask is boolean, i.e. \mathbf{Z} \in \mathbb{C}^{p \times q}\, \wedge \, \mathbf{M} \in \mathbb{B}^{p \times q}

Usage

mat.mask(org, mask, drop.dat=F)
mat.mask(org, mask)
mat.mask(org, mask, drop.dat=T)

Arguments

org

The original data matrix \mathbf{Z} = (z_{ij})_{p \times q} that you want to mask.
Can be numeric, complex or boolean.

mask

The masking matrix \mathbf{M} = (m_{ij})_{p \times q} that you want to use as a mask.
Should be a boolean matrix.
Should have the same dimensions as the org matrix.

drop.dat

Boolean bit defaulting to FALSE.
For all indices i,j, when m_{ij} == \code{T}, then the corresponding z_{ij} remains unchanged.
When drop.dat == F, then \forall m_{ij} = \code{F}, \,\, z_{ij}=0.
When drop.dat == T, then \forall m_{ij} = \code{F}, either z_{ij}=0 or z_{ij} will be completely dropped from the matrix.
Please see details.

Details

drop.dat == F preserves the size of the org and mask data and transfers it to the output.
The unmasked parts, where m_{ij} == \code{F} are at least etched down to 0.
Hence a presumptive bias might be needed in the org matrix to separate actual 0s and etched 0s.
When drop.dat == T, after the etching process, the matrix might be reduced in size.
The algo chooses the largest bounding rectangle of choice which contains the masked results.
The rest of the complementary data outside the rectangle contains only etched 0s and are removed from the result.
Hence drop.dat == T might fail to transfer the size of org or mask to the result.
Please see figure below.
mat.mask.png

Value

The returned value is a matrix which is exactly the type(numeric, complex, boolean) that org was.
The size of the matrix is p \times q (same as mask and org) when drop.dat == F.
The size of the matrix might be different when drop.dat == T.

Author(s)

Chitran Ghosal

Examples

####create a 3 gaussian function
gauss3 <- function(X, Y){
  set.seed(2)
  mu <- runif(n = 6, min = 2, max = 7)
  sd <- runif(n = 6, min = 1, max = 3)
  gauss1 <- gauss(X, mu=mu[1], sig = sd[1], probability = T)*gauss(Y, mu=mu[2], sig = sd[2], probability = T)
  gauss2 <- gauss(X, mu=mu[3], sig = sd[3], probability = T)*gauss(Y, mu=mu[4], sig = sd[4], probability = T)
  gauss3 <- gauss(X, mu=mu[5], sig = sd[5], probability = T)*gauss(Y, mu=mu[6], sig = sd[6], probability = T)
  return(gauss1 + gauss2 + gauss3)
}
####define the X and Y values
X <- seq(0, 10, by=0.05)
Y <- seq(0, 10, by=0.05)
Z <- 0.9*function_2D(X, Y, func = gauss3)

####plot the matrix
image(Z, asp=1.0, main = 'org', col = terrain.colors(1000))

####create the masks
mask1 <- Z > 0.5*max(Z)
mask2 <- Z > 0.75*max(Z)
image(mask1, asp = 1.0, main = 'mask1',col = terrain.colors(1000) )
image(mask2, asp = 1.0, main = 'mask2', col = terrain.colors(1000))

##The mask after dropping points
Z1 <- mat.mask(org = Z, mask = mask1, drop.dat = T)
Z2 <- mat.mask(org = Z, mask = mask2, drop.dat = T)
image(Z1, asp=1.0, main = 'after masking by mask1: points dropped', col = terrain.colors(1000))
image(Z2, asp=1.0, main = 'after masking by mask2: points dropped', col = terrain.colors(1000))


##The mask set without dropping points
Z1 <- mat.mask(org = Z, mask = mask1, drop.dat = F)
Z2 <- mat.mask(org = Z, mask = mask2, drop.dat = F)
image(log(Z1+1), asp=1.0, main = 'after masking by mask1: points NOT dropped', col = terrain.colors(1000))
image(log(Z2+1), asp=1.0, main = 'after masking by mask2: points NOT dropped', col = terrain.colors(1000))

Chitran1987/StatsChitran documentation built on Feb. 23, 2025, 8:30 p.m.