lattice2node {INLA} | R Documentation

## Functions to define mapping between a lattice and nodes

### Description

These functions define mapping in between two-dimensional indices on a lattice and the one-dimensional node representation used in `inla`.

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`.

### Usage

    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)

### Arguments

`nrow` | Number of rows in the lattice.
`ncol` | Number of columns in the lattice.
`irow` | Lattice row index, between `1` and `nrow`
`icol` | Lattice column index, between `1` and `ncol`
`node` | The node index, between `1` and `ncol*nrow`
`a.matrix` | is a matrix to be mapped to a vector using internal representation defined by `inla.lattice2node`
`a.vector` | is a vector to be mapped into a matrix using the internal representation defined by `inla.node2lattice`

### Value

`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.

### Author(s)

Havard Rue hrue@r-inla.org

### See Also

inla

### Examples

    ## 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=""))
        }
    }

    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=""))
        }
    }

    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=""))

    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=""))

    ## 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)
    print(z)
    print(z.vector)

    ## 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)

[Package INLA version 18.07.12 Index]