pkern_mat2vec: Column-vectorization indices

View source: R/pkern_index.R

pkern_mat2vecR Documentation

Column-vectorization indices

Description

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

Usage

pkern_mat2vec(ij, gdim, simplified = TRUE)

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.

Value

integer vector, the vectorized ij indices

Examples

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

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


deankoch/pkern documentation built on Oct. 26, 2023, 8:54 p.m.