Learn R Programming

snapKrig (version 0.0.2)

sk_mat2vec: Column-vectorization indices

Description

Maps matrix indices i, j to a single vectorized index, k

Usage

sk_mat2vec(ij, gdim, simplified = TRUE)

Value

integer vector, the vectorized ij indices

Arguments

ij

n x 2 matrix, the row and column indices

gdim

integer (or vector with first element equal to) the number of rows in the matrix

simplified,

if FALSE, the function returns an n x 1 matrix

Details

Column vectorization (as in base::as.vector) builds a length(mn) vector by stacking the columns of an m X n matrix, with the leftmost column appearing first in the vector and the rightmost column last. Matrix element i,j gets mapped to element k = i + m * (j-1) in the vector. This function returns that index.

ij can be a matrix or a list of length-n vectors 'i' and 'j', or a vector representing a single point at the given row (i) and column (j) number (in that order).

gdim should either be an integer number of rows in the matrix, or a vector of the form c(ni, nj) (the return value of dim for example) in which case its first element is used.

See Also

Other indexing functions: sk_rescale(), sk_sub_find(), sk_sub_idx(), sk_vec2mat()

Examples

Run this code
# define matrix dimensions and look up a specific index
gdim = c(4, 5)
ij = c(i=3, j=2)
sk_mat2vec(ij, gdim)

# display all matrix indices in column-vectorized order
gyx = expand.grid(i=seq(gdim[1]), j=seq(gdim[2]))
result = sk_mat2vec(gyx, gdim)
data.frame(k=result, gyx)

Run the code above in your browser using DataLab