compactToMat.Rd
compactToMat
transforms a matrix from compact, vector form to
a standard matrix. Only symmetric matrices can be stored in this
form, since a compact matrix is stored as a vector with elements
representing the upper triangle of the matrix. This function assumes
the vector does not contain diagonal elements of the matrix.
An example of a matrix stored in compact form is any matrix
generated from the rdist
function with compact=TRUE
.
compactToMat(compactMat, diagVal=0, lower.tri=FALSE, upper.tri=TRUE)
A symmetric matrix stored as a vector containing elements for the lower-triangular
portion of the true matrix (and none of the diagonal elements), as returned by
rdist
with compact=TRUE
.
A number to put in the diagonal entries of the output matrix.
Whether or not to fill in the upper triangle of the output matrix
Whether or not to fill in the lower triangle of the output matrix
The standard form matrix represented by the input compact matrix
rdist
, link{dist}
################
#Calculate distance matrix from compact form:
################
#make a distance matrix
distOut = rdist(1:5, compact=TRUE)
print(distOut)
#> 1 2 3 4
#> 2 1
#> 3 2 1
#> 4 3 2 1
#> 5 4 3 2 1
#note that distOut is in compact form:
print(c(distOut))
#> [1] 1 2 3 4 1 2 3 1 2 1
#convert to standard matrix form:
distMat = compactToMat(distOut)
################
#fast computation of covariance matrix:
################
#generate 5 random points on [0,1]x[0,1] square
x = matrix(runif(10), nrow=5)
#get compact distance matrix
distOut = rdist(x, compact=TRUE)
#evaluate Exponential covariance with range=1. Note that
#Covariance function is only evaluated over upper triangle
#so time is saved.
diagVal = Exponential(0, range=1)
compactCovMat = Exponential(distOut, range=1)
upperCovMat = compactToMat(compactCovMat, diagVal)
lowerCovMat = compactToMat(compactCovMat, diagVal, lower.tri=TRUE, upper.tri=FALSE)
fullCovMat = compactToMat(compactCovMat, diagVal, lower.tri=TRUE, upper.tri=TRUE)
compactCovMat
#> 1 2 3 4
#> 2 0.7615712
#> 3 0.5035015 0.5050630
#> 4 0.4094408 0.4369573 0.7782794
#> 5 0.5253819 0.5329783 0.9452664 0.7657358
lowerCovMat
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.0000000 0.0000000 0.0000000 0.0000000 0
#> [2,] 0.7615712 1.0000000 0.0000000 0.0000000 0
#> [3,] 0.5035015 0.5050630 1.0000000 0.0000000 0
#> [4,] 0.4094408 0.4369573 0.7782794 1.0000000 0
#> [5,] 0.5253819 0.5329783 0.9452664 0.7657358 1
upperCovMat
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 0.7615712 0.5035015 0.4094408 0.5253819
#> [2,] 0 1.0000000 0.5050630 0.4369573 0.5329783
#> [3,] 0 0.0000000 1.0000000 0.7782794 0.9452664
#> [4,] 0 0.0000000 0.0000000 1.0000000 0.7657358
#> [5,] 0 0.0000000 0.0000000 0.0000000 1.0000000
fullCovMat
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.0000000 0.7615712 0.5035015 0.4094408 0.5253819
#> [2,] 0.7615712 1.0000000 0.5050630 0.4369573 0.5329783
#> [3,] 0.5035015 0.5050630 1.0000000 0.7782794 0.9452664
#> [4,] 0.4094408 0.4369573 0.7782794 1.0000000 0.7657358
#> [5,] 0.5253819 0.5329783 0.9452664 0.7657358 1.0000000