lattice2node.RdThese functions define mapping in between two-dimensional indices on a
lattice and the one-dimensional node representation used in inla.
inla.lattice2node.mapping(nrow, ncol)
inla.node2lattice.mapping(nrow, ncol)
inla.lattice2node(irow, icol, nrow, ncol)
inla.node2lattice(node, nrow, ncol)
inla.matrix2vector(a.matrix)
inla.vector2matrix(a.vector, nrow, ncol)Number of rows in the lattice.
Number of columns in the lattice.
Lattice row index, between 1 and nrow
Lattice column index, between 1 and ncol
The node index, between 1 and ncol*nrow
is a matrix to be mapped to a vector using internal
representation defined by inla.lattice2node
is a vector to be mapped into a matrix using the internal
representation defined by inla.node2lattice
inla.lattice2node.mapping returns the hole mapping as a
matrix, and inla.node2lattice.mapping returns the hole mapping as
list(irow=..., icol=...). inla.lattice2node and
inla.node2lattice provide the mapping for a given set of lattice
indices and nodes. inla.matrix2vector provide the mapped vector from
a matrix, and inla.vector2matrix provide the inverse mapped matrix
from vector.
The mapping from node to lattice follows the default R behaviour
(which is column based storage), and as.vector(A) and matrix(a, nrow, ncol) can be used instead of inla.matrix2vector and
inla.vector2matrix.
## write out the mapping using the two alternatives
nrow = 2
ncol = 3
mapping = inla.lattice2node.mapping(nrow,ncol)
for (i in 1:nrow){
for(j in 1:ncol){
print(paste("Alt.1: lattice index [", i,",", j,"] corresponds",
"to node [", mapping[i,j],"]", sep=""))
}
}
#> [1] "Alt.1: lattice index [1,1] correspondsto node [1]"
#> [1] "Alt.1: lattice index [1,2] correspondsto node [3]"
#> [1] "Alt.1: lattice index [1,3] correspondsto node [5]"
#> [1] "Alt.1: lattice index [2,1] correspondsto node [2]"
#> [1] "Alt.1: lattice index [2,2] correspondsto node [4]"
#> [1] "Alt.1: lattice index [2,3] correspondsto node [6]"
for (i in 1:nrow){
for(j in 1:ncol){
print(paste("Alt.2: lattice index [", i,",", j,"] corresponds to node [",
inla.lattice2node(i,j,nrow,ncol), "]", sep=""))
}
}
#> [1] "Alt.2: lattice index [1,1] corresponds to node [1]"
#> [1] "Alt.2: lattice index [1,2] corresponds to node [3]"
#> [1] "Alt.2: lattice index [1,3] corresponds to node [5]"
#> [1] "Alt.2: lattice index [2,1] corresponds to node [2]"
#> [1] "Alt.2: lattice index [2,2] corresponds to node [4]"
#> [1] "Alt.2: lattice index [2,3] corresponds to node [6]"
inv.mapping = inla.node2lattice.mapping(nrow,ncol)
for(node in 1:(nrow*ncol))
print(paste("Alt.1: node [", node, "] corresponds to lattice index [",
inv.mapping$irow[node], ",",
inv.mapping$icol[node],"]", sep=""))
#> [1] "Alt.1: node [1] corresponds to lattice index [1,1]"
#> [1] "Alt.1: node [2] corresponds to lattice index [2,1]"
#> [1] "Alt.1: node [3] corresponds to lattice index [1,2]"
#> [1] "Alt.1: node [4] corresponds to lattice index [2,2]"
#> [1] "Alt.1: node [5] corresponds to lattice index [1,3]"
#> [1] "Alt.1: node [6] corresponds to lattice index [2,3]"
for(node in 1:(nrow*ncol))
print(paste("Alt.2: node [", node, "] corresponds to lattice index [",
inla.node2lattice(node,nrow,ncol)$irow[1], ",",
inla.node2lattice(node,nrow,ncol)$icol[1],"]", sep=""))
#> [1] "Alt.2: node [1] corresponds to lattice index [1,1]"
#> [1] "Alt.2: node [2] corresponds to lattice index [2,1]"
#> [1] "Alt.2: node [3] corresponds to lattice index [1,2]"
#> [1] "Alt.2: node [4] corresponds to lattice index [2,2]"
#> [1] "Alt.2: node [5] corresponds to lattice index [1,3]"
#> [1] "Alt.2: node [6] corresponds to lattice index [2,3]"
## apply the mapping from matrix to vector and back
n = nrow*ncol
z = matrix(1:n,nrow,ncol)
z.vector = inla.matrix2vector(z) # as.vector(z) could also be used
print(mapping)
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
print(z)
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
print(z.vector)
#> [1] 1 2 3 4 5 6
## the vector2matrix is the inverse, and should give us the z-matrix
## back. matrix(z.vector, nrow, ncol) could also be used here.
z.matrix = inla.vector2matrix(z.vector, nrow, ncol)
print(z.matrix)
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6